var myDelay=(function(param){var timer=0;return function(callback,ms){clearTimeout(timer);timer=setTimeout(callback,ms);};}());

function str_replace (search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = Object.prototype.toString.call(r) === '[object Array]',
        sa = Object.prototype.toString.call(s) === '[object Array]';
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

function faq() {
	if (jQuery("body:eq(0).faq").length === 1) {
		jQuery("body:eq(0).faq div[class*='answer']").addClass("answer");
		jQuery("body:eq(0).faq a.faqtoggler:first").addClass("visible");
		jQuery("body:eq(0).faq div[class*='answer']:not(body:eq(0).faq div[class*='answer']:first)").hide();

		jQuery("a.faqtoggler").click(function(){
			jQuery(this).blur();
			if (jQuery(this).hasClass("visible")) {
				jQuery(this).removeClass("visible");
				jQuery(this).parent().next("div.answer").hide();
			} else {
				jQuery(this).addClass("visible");
				jQuery(this).parent().next("div.answer").show();
			}
		});
	}
}

jQuery(document).ready(function(){
	jQuery("body:eq(0)").addClass("jsactive");
	
	jQuery("input[value='Continue']").remove();
	/** slideshow **/
	jQuery("#slideshowproboxwrapper div.slideshowprobox ul").innerfade({
			animationtype: 'fade',
			speed: 750,
			timeout: 5000,
			type: 'sequence',
			containerheight: '384px'
	});
	
	/** K2 item lists **/
	jQuery("body.materiales div.itemContainer").each(function(count){ jQuery(this).addClass("item"+(Number(count)+1)).css({width:'192px'}); });
	jQuery("body.proyectos div.itemContainer").each(function(count){ jQuery(this).addClass("item"+(Number(count)+1)).css({width:'214px'}); });
	jQuery("body.liststyle div.itemContainer, div.itemContainer").each(function(count){ jQuery(this).addClass("item"+(Number(count)+1)).css({width:'192px'}); });
	
	/** menu top **/
	jQuery("ul.menu:eq(0) > li:last").addClass("last");
	
	/** equal height fix for proyectos **/
	if (jQuery('body.proyectos').length === 1) {
		var asideheight = jQuery("body.proyectos div.moduletablesidebar").height();
		var articleheight = jQuery("body.proyectos article").height();
		var max = asideheight;
		if (articleheight > asideheight) max = articleheight;
		jQuery("body.proyectos div.moduletablesidebar, body.proyectos article").css("height", max+'px');
	}
	
	jQuery.fn.superfish.defaults = { 
	    hoverClass:    'sfHover',          // the class applied to hovered list items 
	    pathClass:     'overideThisToUse', // the class you have applied to list items that lead to the current page 
	    pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
	    delay:         800,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
	    animation:     {opacity:'show'},   // an object equivalent to first parameter of jQuery’s .animate() method 
	    speed:         'normal',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
	    autoArrows:    true,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
	    dropShadows:   true,               // completely disable drop shadows by setting this to false 
	    disableHI:     false,              // set to true to disable hoverIntent detection 
	    onInit:        function(){},       // callback function fires once Superfish is initialised – 'this' is the containing ul 
	    onBeforeShow:  function(){},       // callback function fires just before reveal animation begins – 'this' is the ul about to open 
	    onShow:        function(){},       // callback function fires once reveal animation completed – 'this' is the opened ul 
	    onHide:        function(){}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
	};
	
	jQuery('#main ul.menu').superfish({ 
	    delay:       1000,                            // one second delay on mouseout 
	    animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
	    speed:       'fast',                          // faster animation speed 
	    autoArrows:  false,                           // disable generation of arrow mark-up 
		dropShadows: false                            // disable drop shadows 
	});
	
	faq();
	
	jQuery("#pop a.fancybox").fancybox();
	
	jQuery("#onlinequote a").click(function(){
		if (jQuery("#jsquote").hasClass("visible")) {
			// hide
			jQuery("#jsquote").animate({left:'-500px'}).removeClass("visible");
			jQuery("#onlinequote").animate({left:'0'});
		} else {
			// show
			jQuery("#jsquote").show().animate({left:"0"}).addClass("visible");
			jQuery("#onlinequote").animate({left:'500px'});
		}
		return false;
	});
	jQuery("body.id64 table.contentpaneopen:eq(0)").hide();
	jQuery("#fading img").addClass("bg");
	jQuery("#fading").innerfade({
			animationtype: 'fade',
			speed: 1000,
			timeout: 8000,
			type: 'sequence',
			containerheight: '100%'
	});
	
	jQuery("div.itemBody a:has(img)[href$=.jpg]").fancybox();
	
	jQuery("#main a").each(function(){
		var link = "http://mdstestserver.com"+jQuery(this).attr("href");
		var page = document.location.href;
		if (link === page) console.log("link: " + link + " - page: " + page);
		if(link === page) {
			// alert("found curent page");
			jQuery(this).addClass("active");
			jQuery(this).parents("li").removeClass("active");
			jQuery(this).parents("li").addClass("active").attr("id","current");
		}
	});
});

