$(document).ready(function() {

    /*country selector*/
    $('#countries-toggle').toggle(function() {
        $('#countries').stop().animate({ 'height': $('#countries .outside').height() }, 700, 'easeOutExpo');
    }, function() {
        $('#countries').stop().animate({ 'height': 0 }, 700, 'easeOutExpo');
    }).disableTextSelect();

    $('#countries-close').click(function() {
        $('#countries-toggle').trigger('click');
        return false;
    });

    /*tabs*/
    $('.tabs a').click(function() {
        $(this).parents('.tabs').find('td').removeClass('current');
        $(this).parents('td').addClass('current');
        var ids = '';
        $(this).parents('.tabs').find('a').each(function() {
            ids += '#' + $(this).attr('name') + ',';
        });
        $(ids).addClass('hidden');
        $('#' + $(this).attr('name')).removeClass('hidden');
        location.hash = "#tab=" + $(this).attr('name');
        return false;
    });

    /*slide text blocks*/
    $('.dynamic .outside').css('height', 0);
    $('.dynamic .inside').css('position', 'absolute');
    $('.switch a').click(function() {
        $(this).parents('.switch').find('a').toggleClass('hidden');
        if ($(this).attr('name') == 'open') {
            $(this).parents('.node').find('.outside').stop().animate({ 'height': $(this).parents('.node').find('.inside').height() }, 700, 'easeOutExpo');
        }
        else if ($(this).attr('name') == 'close') {
            $(this).parents('.node').find('.outside').stop().animate({ 'height': 0, 'top': 0 }, 700, 'easeOutExpo');
        }
        return false;
    });
    $('.switch-trigger').click(function() {
        $(this).parents('.node').find('.switch a').each(function() {
            if (!$(this).hasClass('hidden')) {
                $(this).trigger('click');
                return false;
            }
        });
        return false;
    });
    $('.bundle .node').hover(
		function() {
		    $(this).addClass('lighting');
		},
		function() {
		    $(this).removeClass('lighting');
		}
	);

    /*gallery*/
    if ($('.nav').hasClass('nav')) $('.nav').pngFix();
    if ($('.gallery').hasClass('gallery')) $('.gallery').gallery();

    /*tree*/
    $('.toggle').click(function() {
        $(this).parents('li').toggleClass('open');
        return false;
    });
    $('.toggle').hover(
		function() {
		    $(this).addClass('hover');
		},
		function() {
		    $(this).removeClass('hover');
		}
	);

    /*scroll logo*/
    $('.next_').click(function() {
        $('.scrollable_').scrollTo('+=592px', 1000);
    });
    $('.prev_').click(function() {
        $('.scrollable_').scrollTo('-=592px', 1000);
    });

    /*modal*/
    $('.open_modal').click(function() {
        $(this).parents('.ns_modal').find('.modal').toggleClass('hidden');
        return false;
    });
    $('.close_modal').click(function() {
        $(this).parents('.ns_modal').find('.modal').addClass('hidden');
        return false;
    });

    /*alt*/
    $('.open_alt').click(function() {
        $(this).parents('.ns_alt').find('.alt').toggleClass('hidden');
        return false;
    });
    $('.close_alt').click(function() {
        $(this).parents('.ns_alt').find('.alt').addClass('hidden');
        return false;
    });

    /*LRC tabs*/
    $('.tabs_ a').click(function() {
        var a = $('.tabs_ a').removeClass('current');
        $(this).addClass('current');
        $('.container').addClass('hidden');
        $('#' + $(this).attr('name')).removeClass('hidden');
        return false;
    });

    $('.subtabs a').click(function() {
        var a = $('.subtabs a').removeClass('current');
        $(this).addClass('current');
        $('.subcont').addClass('hidden');
        $('#' + $(this).attr('name')).removeClass('hidden');
        return false;
    });

    $('.faq .q a').click(function() {
        $(this).parents('li').find('.a').toggleClass('hidden');
        return false;
    });

    /*LRC lightbox*/
    $('.steps .zoom').click(function() {
        var src = $(this).attr('href');
        var img = new Image();

        $(img).load(function() {
            // get site root from script src
            var url = $("script").first().attr("src");
            var siteRoot = url.substring(0, url.indexOf("resources"));
            $(window).htmlPopup({
                'opacity': '0.65',
                'width': img.width + 12,
                'duration': 300,
                'html': '<div style="text-align: center;background:#FFF"><div style="text-align: right; padding:4px 4px 0 0;"><img src="' + siteRoot + 'resources/img/close-popup.gif" id="close-popup" style="cursor: pointer;" alt=""/></div><img style="border: solid 5px white;" src=' + src + ' /></div>'
            });
        });

        img.src = $(this).attr('href');
        return false;
    });

    /*Renew tabs*/
    $('.sections li').click(function() {
        $('.sections li').removeClass('current');
        $(this).addClass('current');
        $('.section').addClass('hidden');
        $('#' + $(this).find('a').attr('name')).removeClass('hidden');
        $('#' + $(this).find('a').attr('name') + '_').removeClass('hidden');
        return false;
    });

    /*load current tab by url*/
    //if($.url.param("tab")) $('a[name='+$.url.param("tab")+']').trigger('click');	
    var hash = location.hash;
    if (hash.indexOf("tab=") != -1) {
        hash = hash.replace(/^#tab=/, "");
        $('a[name=' + hash + ']').trigger('click');
    }

    $('.open-form').click(function() {
        $('.distr-form').addClass('hidden');
        $($(this).attr('name')).removeClass('hidden');
        return false;
    });

    /*load current tab by href*/
    $('a[href*="#tab="]').click(function() {
        var hash = $(this).attr("href");
        hash = hash.replace(/(.)*#tab=/, "");
        $('a[name=' + hash + ']').trigger('click');
    });

    /*show index promo*/
    var id = $.frequency($("#promo").data("view"));
    $.promo(id);


    /*partner selector control*/
    $("a.htmlPopup").click(function() {
        switchPartners();
        var data = $("div#dialog").html();
        $(window).htmlPopup({ 'opacity': '0.65', 'width': 450, 'duration': 300, 'html': data });

        $("#jQuery_popup #partnerDIV ul li").click(function() {
            var id = $(this).attr("id");
            var str = $(this).html();
            setCookie(str, id);
        });
    });

    var affid = checkCookie();
    if (affid) {
        var name = $('#partnerDIV:first ul li[id="' + affid + '"]').first().html();
        setCookie(name, affid);
    }
    else {
        $("a.htmlPopup").click();
    }

});
    function switchPartners() 
    {
        var items = $("#partnerDIV:first ul li");
        var partners = new Array();
        for (var i = 0; i < items.length; i++) {
            partners.push({ 'name': $(items[i]).html(), 'value': $(items[i]).attr("id") });
        }
        partners.sort(function() { return 0.5 - Math.random() });
        setCookie(partners[0].name, partners[0].value);

        $("#partnerDIV ul").html("");
        for (var i = 0; i < partners.length; i++) {
            var str = '<li id="' + partners[i].value + '">' + partners[i].name + '</li>';
            $("#partnerDIV ul").append(str);
        }
    }

    function checkCookie() { 
        var allCookies = document.cookie;
        if (allCookies) {
            if (allCookies.match(new RegExp('affiliateID=([^;]*)', 'g'))) 
            {
                return RegExp.$1;
            }
        }
    }

    function setCookie(str, affid) 
    {
        var cookieEnabled = (navigator.cookieEnabled) ? true : false;
        if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
            document.cookie = "testcookie";
            cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
        }

        if (cookieEnabled) {
            document.cookie = "affiliateID=" + affid + ";path=/;expires=" + (new Date((new Date()).getTime() + (24 * 60 * 60 * 1000)).toGMTString());
            $("#partner").html(str);
        }
        else {
            alert("Please ensure cookies are turned on in your browser settings to be able to choose your partner");
        }
        $('#close-popup').trigger('click');
    }
    
    function downloadClick(item, linkFieldId)
	{
	    var linkField = document.getElementById(linkFieldId)
	    if (linkField!=null)
	    {
		    var href = linkField.value;
		    if (href!=null && href!="")
		    {
			    if (item.href)
			    {
				    item.href= href;
				    return true;
			    }
			    else
				    window.location = href;
		    }
	    }
	    return false;
	}
   
   function getCurrentTab()
   {
        var res = location.hash.replace("#", "");
        if (res)
        {
            res = res.substring(0,1);
            if (isNaN(res))
	            res = "0";
        }
        else
           res = "0";
        return "tab-" + res;
   }


