$(document).ready(function()
{
	var img = new Image();
	img.src = '../images/nyroModal/ajaxLoader.gif';

////////////////////////////////////////////////////////////////
//  Info icons style setup

	$("div#info").mouseover(function(){
		$(this).addClass("ower");
	}).mouseout(function(){
		$(this).removeClass("ower");
	});
	
//  Info icons style setup
////////////////////////////////////////////////////////////////


//	$('#container-1').tabs(3);
//	$('#container-2').tabs(3);
	$('.nav_sec-1 a').click();

	$('a.ext').attr({target: "_blank"});

	$('#bigImage').nyroModal({type: 'image'});
	$('.nyroModal').nyroModal();

    $(".carousel_new").jCarouselLite({
        btnNext: ".next_new",
        btnPrev: ".prev_new",
        auto: 8000,
        speed: 1000,
        circular: true,
        visible: 4
    });
    
    $(".carousel_featured").jCarouselLite({
        btnNext: ".next_featured",
        btnPrev: ".prev_featured",
        auto: 5000,
        speed: 1000,
        circular: true,
        visible: 4
    }); 
    
    $('a.cluetip').cluetip({
    	ajaxCache: true,
    	cluezIndex:       99,
    	fx: {             
        open:       'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'     
        openSpeed:  'normal'
        	}
    });
    
    $('#orderDate').datepick({beforeShowDay: noSunday, showOn: 'both', buttonImageOnly: true, buttonImage: '../templates/oktal_pharma/images/jQueryUI/calendar.gif',  buttonText: 'Odaberite datum...', minDate: '+1', defaultDate: '+1', mandatory: true, navigationAsDateFormat: true, prevText: '< M', nextText: 'M >', changeMonth: false, changeYear: false});
    $('.datepick-trigger').css({'position' : 'relative', 'left' : '-21px', 'top' : '-1px'});
});

function noSunday(date){
    var day = date.getDay();
    return [(day > 0), ''];
}; 

function bookmarksite(title){
	var url = location.href.toString();
	if(url.indexOf("?") > 0 ) url = url.substr(0, url.indexOf("?"));
	
	if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

function setAsHomepage() {
	var url = location.href.toString();
	if(url.indexOf("?") > 0 ) url = url.substr(0, url.indexOf("?"));
	 if (document.all)
	    {
	        document.body.style.behavior='url(#default#homepage)';
	        document.body.setHomePage(url);	 
	        return false;
	    }
	    else if (window.sidebar)
	    {
	    if(window.netscape)
	    {
	         try
	         {  
	            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
	         }  
	         catch(e)  
	         {  
    	        var content = '<br/>Vaš preglednik ne podržava automatsko postavljanje Početne stranice. <br/> Kliknite na ikonu Oktal Pharme lijevo od web adrese i odvucite je na "Home" gumb.';
    	        $.nyroModalManual({
    	        	minHeight: 100,
    	        	content: content
    	        });
	        	 return false;
	         }
	    } 
	    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
	    prefs.setCharPref('browser.startup.homepage',url);
	 }
}

jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 


function setActiveTab(tab, activeTab) {	
	
	if (document.getElementById('link-2').className.search('current') != -1) {
		activeTab = 2;
	}
	else if (document.getElementById('link-3').className.search('current') != -1) {
		activeTab = 3;
	}
	else if (document.getElementById('link-1').className.search('current') != -1) {
		activeTab = 1;
	}
	
	var xmlHttp;
	try {
			// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
		    try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
		    }
		}
	}
	
	xmlHttp.open("POST", "index2.php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	if(tab != activeTab) {
		$('#fragment-' + activeTab).fadeOut("normal", function () {
			$('#fragment-' + tab).fadeIn("normal");
		});
		
		$('#link-' + activeTab).removeClass('current');
		$('#link-' + tab).addClass('current');
		activeTab=tab;
		
		var passData = 'only_page=1&page=shop.saveData&option=com_virtuemart&activeTab=' + activeTab;
		xmlHttp.send(passData);
	}
		
	return false;
}

var activeTabCont=2;
function setActiveTabCont(tabCont, link)
{
	if(tabCont != activeTabCont)
	{

		$('#fragment-' + (tabCont + 3)).addClass('tabs-hide');
		$('#fragment-' + (activeTabCont + 3)).removeClass('tabs-hide');

		$('#link-cont-' + tabCont).addClass('current');
		$('#link-cont-' + activeTabCont).removeClass('current');
		activeTabCont=tabCont;
	}

	return false;
}

function setVisibleCartPage(showID, hideID, totalPage) {
	
	$("#cartPager").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&activePage="+showID+"&totalPage=" + totalPage + "");
	
	$("ul[id='nav_cart'][name='cart"+ hideID +"']").fadeOut("slow", function(){
		$("ul[id='nav_cart'][name='cart"+ showID +"']").fadeIn("slow");
	});
}

function updateQuantity(productID, categoryID, newQuantity, mode, compare) {
	if (compare == 'compare') {
		if ($('#sort_manufacturer').hasClass('current')) {
			var sort = "manufacturer";
		}
		else if ($('#sort_price').hasClass('current')) {
			var sort = "price";
		}
		else {
			var sort = "name";
		}
		$.nyroModalManual({
				minWidth: 230,
				height:550,
				url: "index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&productID="+productID+"&categoryID="+ categoryID +"&newQuantity=" + newQuantity + "&mode=" + mode + "&sort=compare"
		    });
	}
	else {
		if ($('#sort_manufacturer').hasClass('current')) {
			var sort = "manufacturer";
		}
		else if ($('#sort_price').hasClass('current')) {
			var sort = "price";
		}
		else {
			var sort = "name";
		}		
		if (newQuantity == 0) {
			$("li[id='cartItem"+productID+"']").fadeOut("slow");
			$.get("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&productID="+productID+"&categoryID="+ categoryID +"&newQuantity=" + newQuantity + "&mode=" + mode + "&sort=" + sort + "", function(data){
					pom = data.split("|split text|");
					$("#cart").html(pom[0]);
					$("#fullCart").html(pom[1]);
					if ($("#fullCart").html() != null && ($("#fullCart").html().match("orderDate") != null) && ($("#fullCart").html().match("datepick-trigger") == null)) {
						$('#orderDate').datepick({showOn: 'both', buttonImageOnly: true, buttonImage: '../templates/oktal_pharma/images/jQueryUI/calendar.gif',  buttonText: 'Odaberite datum...', minDate: '+1', defaultDate: '+1', mandatory: true, navigationAsDateFormat: true, prevText: '< M', nextText: 'M >', changeMonth: false, changeYear: false});
						$('.datepick-trigger').css({'position' : 'relative', 'left' : '-21px', 'top' : '-1px'});
					}
				}
			);
		}
		else {
			$.get("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&productID="+productID+"&categoryID="+ categoryID +"&newQuantity=" + newQuantity + "&mode=" + mode + "&sort=" + sort + "", function(data){
					pom = data.split("|split text|");
					$("#cart").html(pom[0]);
					$("#fullCart").html(pom[1]);
					$("li[id='cartItem"+productID+"']").fadeIn("slow");
					if ($("#fullCart").html() != null && ($("#fullCart").html().match("orderDate") != null) && ($("#fullCart").html().match("datepick-trigger") == null)) {
						 $('#orderDate').datepick({showOn: 'both', buttonImageOnly: true, buttonImage: '../templates/oktal_pharma/images/jQueryUI/calendar.gif',  buttonText: 'Odaberite datum...', minDate: '+1', defaultDate: '+1', mandatory: true, navigationAsDateFormat: true, prevText: '< M', nextText: 'M >', changeMonth: false, changeYear: false});
						 $('.datepick-trigger').css({'position' : 'relative', 'left' : '-21px', 'top' : '-1px'});
					}
				}
			);
		}
	}
	
	
}

function deleteAllFromCart() {
	$.get("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&deleteAllFromCart=true", function(data){
			pom = data.split("|split text|");
			$("#cart").html(pom[0]);
			$("#fullCart").html(pom[1]);
		}
	);
}

var first = true;
function addToCompareCart(productID, categoryID, mode) {
	$("#compareCart").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&function=compareCartAdd&productID="+productID+"&categoryID="+ categoryID + "", function(){
		$("#minicompare"+productID).fadeIn("slow");
	 });
	if (first) {
		$("li[id='compareHideShow'][name!='" + categoryID + "'], div[id='compareHideShow'][name!='" + categoryID + "']").fadeOut("slow");
		first = false;
	}
}

function deleteFromCompareCart(productID, categoryID, mode) {
	if (mode == 'last') {
		first = true;
		$("li[id='compareHideShow'][name!='" + categoryID + "'], div[id='compareHideShow'][name!='" + categoryID + "']").fadeIn("slow");
	}
	$("#minicompare"+productID).fadeOut("slow");
	$("#compareCart").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&function=compareCartDelete&productID="+productID+"");
}

function sortCartBy(mode) {
	 $("#fullCart").fadeOut("normal");
	$("#fullCart").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&sortCartBy="+mode+"", function(){
  $("#fullCart").fadeIn("normal");
 });
	$('#sort_name').removeClass('current');
	$('#sort_manufacturer').removeClass('current');
	$('#sort_price').removeClass('current');
	
	$('#sort_' + mode).addClass('current');
}

function sortListBy(mode, md5) {
	 $("#fullList").fadeOut("normal");
	$("#fullList").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&sortListBy="+mode+"&md5=" + md5, function(){
 $("#fullList").fadeIn("normal");
});
	$('#sort_name').removeClass('current');
	$('#sort_manufacturer').removeClass('current');
	$('#sort_price').removeClass('current');
	
	$('#sort_' + mode).addClass('current');
}

function sortOrderBy(mode, orderID) {
	 $("#fullOrder").fadeOut("normal");
	$("#fullOrder").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&sortOrderBy="+mode+"&order_id=" + orderID, function(){
$("#fullOrder").fadeIn("normal");
});
	$('#sort_name').removeClass('current');
	$('#sort_manufacturer').removeClass('current');
	$('#sort_price').removeClass('current');
	
	$('#sort_' + mode).addClass('current');
}

function addToSupplyList() {
	var name = $("#buy_list_name_input").attr("value");
	$("#shoppingListNumber").fadeOut("slow");
	$.ajax(
		{	type: "GET",
			url: "index2.php",
			data: "only_page=1&page=shop.saveData&option=com_virtuemart&function=addToSupplyList&name=" + name, 
			success: function(msg){
				$("#shoppingListNumber").text(msg);
				$("#shoppingListNumber").fadeIn("slow");
			}
		}
	);
}

function deleteFromSupplyList(date, limitstart) {
	$("#shoppingListNumber").fadeOut("slow", function(){
		var number = parseInt($("#shoppingListNumber").text());
		$("#shoppingListNumber").text(number - 1);
		$("#shoppingListNumber").fadeIn("slow");
	});
	$("#lists").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&function=deleteFromSupplyList&date=" + date+"&limitstart=" + limitstart);
}

function updateCartAddList(md5) {
	$("#cart").fadeTo('slow',0);
	$.get("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&function=updateCartAddList&md5="+ md5 + "", function(data){
		$("#cart").html(data);
		$("#cart").fadeTo('slow',1);
	});			
}

function deleteFromList(md5, productID) {
	$("li[id='listItem"+productID+"']").fadeOut("slow");
	$.get("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&productID="+productID+"&md5="+ md5 +"&function=deleteFromList" + "", function(data){
		if ($('#sort_price').hasClass("current")) {
			$("#fullList").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&sortListBy=price&md5=" + md5);
		}
		else if ($('#sort_manufacturer').hasClass("current")) {
			$("#fullList").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&sortListBy=manufacturer&md5=" + md5);
		}
		else {
			$("#fullList").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&sortListBy=name&md5=" + md5);
		}
	});	
}

function updateCartAddOrder(order_id) {
	$("#cart").fadeTo('slow',0);
	$.get("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&function=updateCartAddOrder&order_id="+ order_id + "", function(data){
		$("#cart").html(data);
		$("#cart").fadeTo('slow',1);
	});			
}

function alignCompare() {
	var h2Array = $("#comparator_main h2");
	var producerArray = $("#comparator_main .producer");
	var descArray = $("#comparator_main .desc");
	var effectsArray = $("#comparator_main .effects");
	var compositionArray = $("#comparator_main .composition");
	var indicationsArray = $("#comparator_main .indications");
	var side_effectsArray = $("#comparator_main .side_effects");
	var method_of_applicationArray = $("#comparator_main .method_of_application");
	var dosageArray = $("#comparator_main .dosage");
	var packagingArray = $("#comparator_main .packaging");
	var removeeffectsArray = $("#comparator_main .removeeffects");
	var removecompositionArray = $("#comparator_main .removecomposition");
	var removeindicationsArray = $("#comparator_main .removeindications");
	var removeside_effectsArray = $("#comparator_main .removeside_effects");
	var removemethod_of_applicationArray = $("#comparator_main .removemethod_of_application");
	var removedosageArray = $("#comparator_main .removedosage");
	var removepackagingArray = $("#comparator_main .removepackaging");	
	var maxh2 = $(h2Array[0]).height();
	var maxproducer = $(producerArray[0]).height();
	var maxdesc = $(descArray[0]).height();
	var maxeffects = effectsArray.length > 0 ? $(effectsArray[0]).height() : 0;
	var maxcomposition = compositionArray.length > 0 ? $(compositionArray[0]).height() : 0;
	var maxindications = indicationsArray.length > 0 ? $(indicationsArray[0]).height() : 0;
	var maxside_effects = side_effectsArray.length > 0 ? $(side_effectsArray[0]).height() : 0;
	var maxmethod_of_application = method_of_applicationArray.length > 0 ? $(method_of_applicationArray[0]).height() : 0;
	var maxdosage = dosageArray.length > 0 ? $(dosageArray[0]).height() : 0;
	var maxpackaging = packagingArray.length > 0 ? $(packagingArray[0]).height() : 0;
	for (var i=1; i<h2Array.length; i++) {			
		if ($(h2Array[i]).height() > maxh2) {
			maxh2 = $(h2Array[i]).height();
		}
		if ($(producerArray[i]).height() > maxproducer) {
			maxproducer = $(producerArray[i]).height();
		}
		if ($(descArray[i]).height() > maxdesc) {
			maxdesc = $(descArray[i]).height();
		}
		if ($(effectsArray[i]).height() > maxeffects) {
			maxeffects = $(effectsArray[i]).height();
		}
		if ($(compositionArray[i]).height() > maxcomposition) {
			maxcomposition = $(compositionArray[i]).height();
		}
		if ($(indicationsArray[i]).height() > maxindications) {
			maxindications = $(indicationsArray[i]).height();
		}
		if ($(side_effectsArray[i]).height() > maxside_effects) {
			maxside_effects = $(side_effectsArray[i]).height();
		}
		if ($(method_of_applicationArray[i]).height() > maxmethod_of_application) {
			maxmethod_of_application = $(method_of_applicationArray[i]).height();
		}
		if ($(dosageArray[i]).height() > maxdosage) {
			maxdosage = $(dosageArray[i]).height();
		}
		if ($(packagingArray[i]).height() > maxpackaging) {
			maxpackaging = $(packagingArray[i]).height();
		}
	}
	for (var j=0; j<h2Array.length; j++) {
		$(h2Array[j]).height(maxh2);
		$(producerArray[j]).height(maxproducer);
		$(descArray[j]).height(maxdesc);
		$(effectsArray[j]).height(maxeffects);
		$(compositionArray[j]).height(maxcomposition);
		$(indicationsArray[j]).height(maxindications);
		$(side_effectsArray[j]).height(maxside_effects);
		$(method_of_applicationArray[j]).height(maxmethod_of_application);
		$(dosageArray[j]).height(maxdosage);
		$(packagingArray[j]).height(maxpackaging);
	}
	if (effectsArray.length == removeeffectsArray.length/2) {
		$("#comparator_main .removeeffects").remove();
	}
	if (compositionArray.length == removecompositionArray.length/2) {
		$("#comparator_main .removecomposition").remove();
	}
	if (indicationsArray.length == removeindicationsArray.length/2) {
		$("#comparator_main .removeindications").remove();
	}
	if (side_effectsArray.length == removeside_effectsArray.length/2) {
		$("#comparator_main .removeside_effects").remove();
	}
	if (method_of_applicationArray.length == removemethod_of_applicationArray.length/2) {
		$("#comparator_main .removemethod_of_application").remove();
	}
	if (dosageArray.length == removedosageArray.length/2) {
		$("#comparator_main .removedosage").remove();
	}
	if (packagingArray.length == removepackagingArray.length/2) {
		$("#comparator_main .removepackaging").remove();
	}
}

function categoryListChanged() {
	var selValue = $("#category_list").val();
	if (selValue != "") {
		$("#category_keyword").load("index2.php?only_page=1&page=shop.saveData&option=com_virtuemart&getCategoryKeywords=" + selValue, function(){
			 $("#category_keyword").fadeIn("slow");
		});
	}
	else {
		$("#category_keyword").fadeOut("slow");
	}
}

function search() {
	var selValue = $("#category_keyword :selected").text();
	if (selValue != "") {
		$("#search_input").attr("value", '"'+selValue+'"'); 
		$("#search").submit(); 
	}	
}

function normalSelected() {
	$("#timePeriod").fadeIn("slow");
	$("#orderDate").fadeIn("slow");
	$('.datepick-trigger').fadeIn("slow");
}

function quickSelected() {
	$("#timePeriod").fadeOut("slow");
	$("#orderDate").fadeOut("slow");
	$('.datepick-trigger').fadeOut("slow");
}
