﻿/* ===================================================================================================================*/
/* suckerfish menu */
$(document).ready(function() {

	//$('#header').supersleight();
	$('body').supersleight();

	suckerFish();
	//tooltip();
	//imagePreview();
});

function mmmm() {
	imagePreview();
}
function suckerFish() {
	$("ul.sf-menu").supersubs({
		minWidth: 174,   // minimum width of sub-menus in px units 
		maxWidth: 174,   // maximum width of sub-menus in px units 
		extraWidth: 0     // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family
	}).superfish({
		delay: 800,
		animation: { opacity: '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
	}).find('ul').bgIframe({ opacity: 'show' });
};


/* CARROUSEL 
=============================================*/

var globalTimeout = 5000; // change slide after x milliseconds
var carrouselOnPause = false; // boolean to check if carrousel is on pause or not

function carrousel() {
	// get all elements 
	var buttons = $('#home_buttons').children();
	var slides = $('#home_slides').children();

	// show first slide, when it has slides
	if (slides.length > 0) {
		
		// show first
		carrouselHover(buttons, slides, 0, "fast");

		var but = $('.home_button').hover(function() {
			// mouseon 
      		carrouselHover(buttons, slides, but.index(this), 'fast');
		},function() {
			// mouseout
      		// does nothing for now 
		});

        // carrousel on pause when mouse is on it
		$('.home_slidecontainer').hover(function() {
		    // mouseon
		    carrouselPause(buttons, slides);
		}, function() {
		    // mouseout
		    carrouselPause(buttons, slides);
		});
		
		//start timeout 
		carrouselTimeout = setTimeout(function() { carrouselNext(buttons, slides, 0, 0);}, globalTimeout);
	}
}

function carrouselHover(buttons, slides, x, speed) {
	var nohide = 0;

	if(speed != "fast" && speed != "slow") {
		alert("Please, set proper speed in carrousel");
	}
	
	// loop through all slides
	for (var i = 0; i < slides.length; i++) {
	
		// fadeout the active slide, but only if the new button is not the current one 
		if ($(slides[i]).is(':visible')) {
			if (i != x) {
				$(slides[i]).fadeOut(speed);
			} else {
				nohide=1
			}
		} else {
			$(slides[i]).hide();
		}

		$(buttons[i]).removeClass('active');

	};
	if (nohide == 0) {
		$(slides[x]).fadeIn(speed);
	}
	$(buttons[x]).addClass('active');
}

function carrouselNext(buttons, slides, current, last) {

	if ((current + 1) < slides.length) {
		current = current + 1;
		last = current - 1;
	} else {
		current = 0;
		last = slides.length - 1;
	}

	carrouselHover(buttons, slides, current, 'slow');

	carrouselTimeout = setTimeout((function() {
		carrouselNext(buttons, slides, current, last);
	}), globalTimeout);

}

function carrouselPause(buttons, slides) {
    if (carrouselOnPause == false) {
        clearTimeout(carrouselTimeout);
        carrouselOnPause = true;
    }
    else {
        // set current
        var current = 0;
        for (var i = 0; i < buttons.length; i++) {
            if ($(buttons[i]).hasClass('active')) {
                current = i;
            }
        }
        // set last
        if (current > 0) {
            var last = current - 1;
        } else {
            var last = slides.length - 1;
        }
        carrouselTimeout = setTimeout((function() {
            carrouselNext(buttons, slides, current, last);
        }), globalTimeout);
        carrouselOnPause = false;
    }
}

/* HARMONICA 
==================================================*/
function toggleHarmonica(titleId, textId, title, useSlider, url) {
    if ($("#" + textId).is(":hidden")) {
        // Google Analytics code
        registerGA(url + "?" + title);
        
        document.getElementById(titleId).className = "title open";
        if (useSlider) {
            $("#" + textId).animate({ "opacity": "1", "height": "toggle" }, "slow");
        } else {
            $("#" + textId).show();
        }
    } else {
        document.getElementById(titleId).className = "title closed";
        if (useSlider) {
            $("#" + textId).animate({ "opacity": "0", "height": "toggle" }, "slow");
        } else {
            $("#" + textId).hide();
        }
    }
}

/* HARMONICAIFRAME 
==================================================*/
function toggleHarmonicaIFrame(titleId, textId, title, useSlider, url) {
    if ($("#" + textId).is(":hidden")) {
        // Google Analytics code
        registerGA(url + "?" + title);

        document.getElementById(titleId).className = "title open";
        if (useSlider) {
            $("#" + textId).animate({ "opacity": "1", "height": "toggle" }, "slow");
        } else {
            $("#" + textId).show();
        }
    } else {
        document.getElementById(titleId).className = "title closed";
        if (useSlider) {
            $("#" + textId).animate({ "opacity": "0", "height": "toggle" }, "slow");
        } else {
            $("#" + textId).hide();
        }
    }
}

/* click with enter 
==================================================*/
function doClick(buttonName, e) {
	//the purpose of this function is to allow the enter key to 
	//point to the correct button to click.
	var key;
	if (window.event) {
		key = window.event.keyCode;     //IE
	} else {
		key = e.which;      //firefox
	}
	if (key == 13) {
		//Get the button the user wants to have clicked
		var btn = document.getElementById(buttonName);
		if (btn != null) { //If we find the button click it
			newBtnName = buttonName.replace(/_/g, "$");
			if (DES_BtnClick(buttonName)) {
				__doPostBack(newBtnName, e)
			}
			event.keyCode = 0
		}
	}
}

function doButtonClick(buttonName, e) {
	//the purpose of this function is to allow the enter key to 
	//point to the correct button to click.
	var key;
	if (window.event) {
		key = window.event.keyCode;     //IE
	} else {
		key = e.which;      //firefox
	}
	if (key == 13) {
		//Get the button the user wants to have clicked
		var btn = document.getElementById(buttonName);
		if (btn != null) { //If we find the button click it
			//btn.click();
			event.keyCode = 0
		}
	}
}

/* GOOGLE ANALYTICS 
==================================================*/
function registerGA(url) {
    try {
        pageTracker._trackPageview(url);
    } catch (err) {
        setTimeout(function() { registerGA(url); }, 500); 
    }
}

/* ADH 
==================================================*/
function toggleGender(genderId, age, pregnant, breastfeeding) {
    var gender = "m";
    tblGender = document.getElementById(genderId);
    rblGender = document.getElementsByTagName('input');
    for (var i = 0; i < rblGender.length; i++) {
        if (rblGender[i].type == "radio") {
            if (rblGender[i].checked) gender = rblGender[i].value;
        }
    }
    if (gender == "m" || document.getElementById(age).value > 50) {
        document.getElementById('trPregnant').style.display = 'none';
        document.getElementById('trBreastfeeding').style.display = 'none';
        document.forms[0].elements[pregnant].checked = false;
        document.forms[0].elements[breastfeeding].checked = false;
    }
    else {
        document.getElementById('trPregnant').style.display = '';
        document.getElementById('trBreastfeeding').style.display = '';
    }
}

/* GENERAL 
==================================================*/
function getElementPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}