jQuery(function () {
    
    jQuery("body").click(function () {
        jQuery(".select-widget ul").hide();
    });

    /* lightbox images actions */
    jQuery(".image-small").click(function () {
        jQuery(".image-small").removeClass("active");
        jQuery(this).addClass("active");
        jQuery("#image-big").attr("src", jQuery(this).children("em").text());
        jQuery(".image-big em").text(jQuery(this).children("strong").text());
    });

    if (jQuery.browser.msie && jQuery.browser.version == "6.0") { }
    else { jQuery("input.custom-check").checkWidget(); }


    // !/===> Spotlight accordion
    jQuery("#main ul.accordion > li .cont").addClass("invisible");
    jQuery("#main ul.accordion > li > .acc").click(function () {
        var jQueryel = jQuery(this);
        var jQueryli = jQueryel.parent("li");
        var jQueryul = jQueryli.parent("ul");
        var jQueryacc = jQueryli.children(".cont");
        var jQueryoli = jQueryul.children("li").not(jQueryli);
        var jQueryoacc = jQueryul.children("li").not(jQueryli).children(".cont");

        // Close all open
        jQueryoli.removeClass("open");
        jQueryoacc.slideUp(300, function () {
            if (jQuery(this).is(":visible")) {
                jQuery(this).attr("style", "").removeClass("invisible").addClass("visible");
            } else {
                jQuery(this).attr("style", "").removeClass("visible").addClass("invisible");
            }
        });
        jQueryacc.slideToggle(300, function () {
            if (jQuery(this).is(":visible")) {
                jQuery(this).attr("style", "").removeClass("invisible").addClass("visible");
            } else {
                jQuery(this).attr("style", "").removeClass("visible").addClass("invisible");
            }
        });
        jQueryli.toggleClass("open");

        return false;
    });

    // dropdowns
    jQuery("#nav > li").hover(
	   function () { jQuery(this).addClass("hover"); }
	  , function () { jQuery(this).removeClass("hover"); }
	 );

    jQuery("#quick-search input[name=search]").defaultVal();

    // footer arrow
    jQuery(".link-arrow").click(function () {
        jQuery(".toogle").slideFadeToggle("slow");
        jQuery(".link-arrow").toggleClass("link-arrow-2");

        return false;
    });

    // header countrys show/hide
    jQuery('.aveva-countrys').click(function () {
        jQuery('#countrys').slideToggle();
        jQuery(this).toggleClass("open");
    });

    // fix last li in countrys	
    jQuery(".countrys-list ul li:last-child").addClass("no-border");

    //Easyslider height-fix function
    ; (function (jQuery) {
        jQuery.fn._easySliderProxy = jQuery.fn.easySlider;
        jQuery.fn.easySlider = function (options) {
            var defaults = {
                dynamicHeight: false,
                preload: true,
                prevId: 'prevBtn',
                nextId: 'nextBtn',
                currentId: 'current'
            };
            var options = jQuery.extend(defaults, options);
            return jQuery(this).each(function () {
                var buildPlugin = function () {
                    jQuery(this)._easySliderProxy(options);
                    if (options.dynamicHeight) {
                        var jQueryevSlider = jQuery(this);
                        var jQueryevContainer = jQueryevSlider.parent();
                        jQueryevContainer.find('#' + options.nextId + ' a,#' + options.prevId + ' a').click(function () {
                            var pos = parseInt(jQueryevContainer.find("#" + options.currentId).text()) || 1;
                            jQueryevSlider.animate({
                                height: jQueryevSlider.find("li").eq(pos).height()
                            }, { queue: false });
                        });
                        var pos = parseInt(jQueryevContainer.find("#" + options.currentId).text()) || 1;
                        jQueryevSlider.css({ height: jQueryevSlider.find("li").eq(pos).height() }, { queue: false });
                    }
                };
                if (options.preload == true) {
                    var jQueryimageElements = jQuery(this).find("img");
                    var loadedImages = 0;
                    var totalImages = jQueryimageElements.length;
                    var jQueryself = jQuery(this);
                    jQueryimageElements.each(function () {
                        jQuery(this).load(function () {
                            loadedImages++;
                            if (loadedImages == totalImages) {
                                buildPlugin.call(jQueryself);
                            }
                        });
                        if (this.complete || jQuery.browser.msie) {
                            jQuery(this).trigger('load');
                        }
                    });
                    if (totalImages == 0) {
                        buildPlugin.call(jQueryself);
                    }
                }
                else {
                    buildPlugin.call(jQuery(this));
                }
            });
        };
    })(jQuery);


    jQuery('#slider-events').easySlider({
        auto: false,
        continuous: true,
        nextId: "btn-next",
        prevId: "btn-prev",
        countId: "events-count",
        currentId: "events-current",
        dynamicHeight: true
    });

    jQuery("#slider-news").easySlider({
        auto: false,
        continuous: true,
        nextId: "btn-next-news",
        prevId: "btn-prev-news",
        countId: "news-count",
        currentId: "news-current",
        dynamicHeight: true
    });

    jQuery("a.cap").fancybox({
});


jQuery("#login-action").fancybox({
    'autoScale': false,
    'type': 'iframe',
    'autoDimensions': false,
    'width': 485,
    'height': 525
});

jQuery("#link-forgotten").fancybox({
    'autoScale': false,
    'type': 'iframe',
    'autoDimensions': false,
    'width': 485,
    'height': 260
});

jQuery(".acc-item .title,a.fancy").fancybox({
    'autoScale': false,
    'type': 'iframe',
    'autoDimensions': false,
    'width': 928,
    'height': 575
});

jQuery('.video_lightbox').fancybox({
    'autoScale': false,
    'type': 'iframe',
    'autoDimensions': false,
    'width': 628,
    'height': 474
});

/* tabs */
jQuery("#tabs .tab-content").hide();
jQuery("#tabs .tab-content").eq(0).show();
jQuery("#tabs ul li").eq(0).addClass("active");

jQuery('#tabs ul li a').click(function () { //When any link is clicked
    jQuery('#tabs ul li').removeClass('active'); // Remove active class from all links
    jQuery(this).parent().addClass('active'); //Set clicked link class to active
    jQuery(".tab-content").hide(); // Hide all divs
    var currentTab = jQuery(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
    jQuery(currentTab).show(); // Show div with id equal to variable currentTab
    return false;
}); //end click(function)

/* tabs-2 */
jQuery("#tabs-2 .tab-content").addClass('invisible');
jQuery("#tabs-2 .tab-content").eq(0).removeClass('invisible').addClass('visible');
jQuery("#tabs-2 ul li").eq(0).addClass("active");
jQuery('#tabs-2 .clicker').click(function () { //When any link is clicked
    jQuery('#tabs-2 ul li').removeClass('active'); // Remove active class from all links
    jQuery(this).parent().addClass('active'); //Set clicked link class to active
    jQuery(".tab-content").removeClass('visible').addClass('invisible'); // Hide all divs
    var currentTab = jQuery(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
    jQuery(currentTab).removeClass('invisible').addClass('visible'); // Show div with id equal to variable currentTab
    return false;
}); //end click(function)

/* tabs-3 */
jQuery("#tabs-3 .tab-content").hide();
jQuery("#tabs-3 .tab-content").eq(0).show();
jQuery("#tabs-3 ul li").eq(0).addClass("active");
jQuery('#tabs-3 ul li a').click(function () { //When any link is clicked
    jQuery('#tabs-3 ul li').removeClass('active'); // Remove active class from all links
    jQuery(this).parent().addClass('active'); //Set clicked link class to active
    jQuery(".tab-content").hide(); // Hide all divs
    var currentTab = jQuery(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
    jQuery(currentTab).show(); // Show div with id equal to variable currentTab
    return false;
}); //end click(function)

});

// slide with fade
jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};


/* ------------------------------------------------------------------------------------------------------------------------------------
/* !===> Div Links */
/* ------------------------------------------------------------------------------------------------------------------------------------
 * Div link - searches the element for anchor tags and adds the first one as the click action
 * ------------------------------------------------------------------------------------------------------------------------------------ */
(function (jQuery) {
 jQuery.fn.divlink = function(params) {
  // Default options
  var params = jQuery.extend({
    
  }, params);
  
  jQuery(this).each(function() {
   var jQueryfirst_link = jQuery(this).find("a");
   
   jQuery(this).data('first_link', jQueryfirst_link.attr("href"));
   jQuery(this).click(function() {
	//console.log(jQuery(this).data('first_link'));						  
    document.location = jQueryfirst_link.attr("href");
   });
   jQuery(this).css("cursor", "pointer");
   jQuery(this).hover(
     function() { jQuery(this).addClass("hover"); }
    ,function() { jQuery(this).removeClass("hover"); }
   );
  }); //end each()
  
 }; //end function()
})(jQuery);


/* ------------------------------------------------------------------------------------------------------------------------------------
/* !===> Default Input Value */
/* ------------------------------------------------------------------------------------------------------------------------------------ */
(function (jQuery) {
 jQuery.fn.defaultVal = function(params) {
  // Default options
  var params = jQuery.extend({
    defaultOverride: false
  }, params);
  
  jQuery(this).each(function() {
   var jQueryel = jQuery(this);
   
   // Remember the default value
   if (params.defaultOverride) jQueryel.data('default_value', params.defaultOverride);
   else       jQueryel.data('default_value', jQueryel.val());
   
   // Focus and blur actions
   jQueryel.focus(function() {
    jQueryel.addClass('focus');
    if ( ! jQueryel.data('not_default')) {
     jQueryel.data('not_default', 1);
     jQueryel.val('');
    } //end if
   }).blur(function() {
    if ( ! jQueryel.val()) {
     if (jQueryel.data('default_value')) jQueryel.val( jQueryel.data('default_value') );
     jQueryel.data('not_default', 0);
    } //end if
   });
   
  }); //end each()
 }; //end function()
})(jQuery);

// Check widget
(function (jQuery) {
	jQuery.fn.checkWidget = function(params) {
		// Default options
		var params = jQuery.extend({
			 
		}, params);
		
		jQuery(this).each(function() {
			var jQueryel 	= jQuery(this);
			var jQuerylabel 	= jQueryel.parent().children("label");
			var jQueryparent = jQueryel.parent()
			
			// Hide the checkbox
			jQueryel.css({
				 position: 	"absolute"
				,top: 		0
				,left: 		-6000
			});
			
			// Add click event
			jQueryel.change(function() {
				//alert("Change...");
				if (jQueryel.is(":checked")) {
					jQueryparent.addClass("on");
				} else {
					jQueryparent.removeClass("on");
				} //end if
			});
			
			// Init setup
			if (jQueryel.is(":checked")) {
				jQueryparent.addClass("on");
			} else {
				jQueryparent.removeClass("on");
			} //end if
			
		}); //end each()
		
	}; //end checkWidget()
})(jQuery);

// Select widget
(function (jQuery) {
	jQuery.fn.selectWidget = function(params) {
		// Default options
		var params = jQuery.extend({
			  onSelect: 	false
			 ,href: 		false
			 ,error: 		false
			 ,onChange:		false
			 ,class_name: 	""
		}, params);
		
		jQuery(this).each(function() {
			// Read combo data
			var jQuerycombo 		= jQuery(this);
			var init_label 	= "-";
			var init_val 	= "";
			var class_error = "";
			
			// Hide the combobox
			jQuerycombo.hide();
			
			if(params.error)  class_error = " error";
			
			// First we prepare the required HTML
			var jQueryel 	= jQuery('<div class="select-widget '+params.class_name+' '+class_error+'"></div>');		jQuerycombo.after(jQueryel);
			var jQueryhandle = jQuery('<p class="selected"><a href="#">'+init_label+'</a></p>'); 	jQueryel.append(jQueryhandle);
			var jQuerylist 	= jQuery('<ul></ul>');
			
			// Add options
			jQuerycombo.children('option').each(function() {
				// Selected class
				var sel_class = "";
				if (jQuery(this).attr("value") == jQuerycombo.val()) sel_class = ' class="sel"';
				
				// Prepare HTML
				var html = jQuery(this).text();
				html = html.replace('*(', '<strong>(');
				html = html.replace(')*', ')</strong>');
				html = html.replace('_(', '<em>(');
				html = html.replace(')_', ')</em>');
				
				// Append to list
				jQuerylist.append('<li'+sel_class+'><a href="#" rel="'+jQuery(this).attr("value")+'">'+html+'</a></li>');
			});
			
			// And add to list
			jQueryel.append(jQuerylist);
			
			// Add rounded
			
			
			// Rest of options
			var href 			= params.href;
			var selected_value 	= 0;
			var init 			= true;
			
			// Hide the list and align it
			jQuerylist.hide().css({
				top: jQueryhandle.height()-3
			});
			
			// Add scroll if more then 5 elements
			if(jQuerylist.height() > 105) {
				jQuerylist.css({
					"overflow":"auto",
					"height":"105px"
				});  
			}
			
			// Add click event on handle
			jQueryhandle.children("a").click(function() {
				jQuery(".select-widget ul").not(jQuerylist).hide();													 	
				jQueryel.toggleClass("select-widget-open");
				jQuerylist.toggle();
				return false;
			});
			
			// Add click event on select elements
			jQuerylist.children("li").children("a").click(function() {
				var label 		= jQuery(this).html();
				selected_value 	= jQuery(this).attr("rel");
				
				// Set values
				jQueryhandle.children("a").html(label);
				jQuerycombo.val(selected_value);
				
				// Hide the list and open class
				jQueryel.removeClass("select-widget-open");
				jQuerylist.hide();
				
				// Redirect on select
				if (init == false && params.onSelect == "submit" && href) {
					document.location = href+"?"+jQuerycombo.attr("name")+"="+selected_value;
				} // end if
				
				if(params.onChange) {
					// product detail show hide
					jQuery(".con-texts").hide();
					jQuery("#con-text-"+selected_value).show();
				}				
				
				return false;
			});
			
			// Select initial if it exists
			jQuerylist.children("li.sel").children("a").click();
			
			// Reset init
			init = false;
			
			// Add blur action
			jQueryel.blur(function() {
				jQueryel.removeClass("select-widget-open");
				jQuerylist.hide();
			});
			
		}); //end each()
		
	}; //end selectWidget()
})(jQuery);

// fancybox direct linking extension
jQuery(document).ready(function(){
	if(window.location.hash.length > 0 && window.location.hash.substring(1).length > 0){
		var prop = ["default",""];
		if(window.location.hash.substring(1).indexOf(":")!=-1){
			prop = window.location.hash.substring(1).split(":");
		} else {
			prop[1]=window.location.hash.substring(1);
		}
		if(prop[0].toLowerCase()=="default"){
			var link = jQuery("a[name="+prop[1]+"]");
			if(link.length){
				var parent = link.parents("li");
				if(!parent.hasClass("open")){
					parent.find("a.acc:eq(0)").click();
				}
				jQuery(document).scrollTop(link.offset().top);
			}
		} else if(prop[0].toLowerCase()=="open"){
			jQuery("a[name="+prop[1]+"]").click();
		} else if(prop[0].toLowerCase()=="scroll"){
			var link = jQuery("a[name="+prop[1]+"]");
			if(link.length){
				var parent = link.parents("li");
				if(!parent.hasClass("open")){
					parent.find("a.acc:eq(0)").click();
				}
				jQuery(document).scrollTop(link.offset().top);
				link.click();
			}
		}
		
	}
		
});
