﻿  /*
	OnLoad
*/

var itemCount = 0;
var myBrowser;
var isIE6;

$ (function() { 

    offerPanelsList = $("li.Category");
    offerPanelsList.hide();
    offerPanelsList.filter(":first").show();
    linksList = $("#offersNavList li a");
    
    var mywidth = $("#offersNavList li").size() * 124;
    
    var listItems = $("#offersNavList li").size();
    
    $('#offersNav').css('width', mywidth);
    $(".liCategoryListFirst").addClass("selected");
    
    if(listItems <= 7) {
        $("#imgScrollLeft").animate({opacity: 0}, 100 );
        $("#imgScrollRight").animate({opacity: 0}, 100 );
        
    } else {
        $("#imgScrollLeft").animate({opacity: 0}, 100 );
        $("#imgScrollRight").animate({opacity: 1}, 100 );
        
        $(".scrollLeft").bind('click', function(){
              $(this).scrollLeft();
              return false;
        });

        $(".scrollRight").bind('click', function(){
              $(this).scrollRight();
              return false;
        });
    }
    
    //get a function to work when tabs clicked *MT //
    jQuery.fn.extend({
    
        tabTrigger: function(){
        
            thisId = $(this).attr("id");
            createCookie('activeTab',thisId,20);
            
            $("#offersNavList li a").each(function() {
                $(this).parent("li").removeClass("selected");
            });
            
            var myHref = $(this).attr("href");
            $(this).parent("li").addClass("selected");
            
            myTarget = "li" + myHref.substring(1);
            $("li.Category").each(function() {
                if($(this).hasClass(myTarget)) {
                    $(this).css("display", "block");
                } else {
                    $(this).css("display", "none");
                }
             });
            return false;
        },
        
        scrollRight: function(){
        
            if(itemCount < listItems - 7) {
                
                $("#offersNav").animate({left: '-=123'}, 500);
                itemCount++;
                if(itemCount == listItems - 7 ){
                    $("#imgScrollLeft").animate({opacity: 1}, 100 );
                    $("#imgScrollRight").animate({opacity: 0}, 100 );
                }
            } else {
                $("#imgScrollLeft").animate({opacity: 1}, 100 );
                $("#imgScrollRight").animate({opacity: 0}, 100 );
            };
        },
        
        scrollLeft: function(){
        
            if(itemCount > 0){
                $("#offersNav").animate({left: '+=123'}, 500);
                itemCount--;
                if(itemCount == 0 ){
                   $("#imgScrollLeft").animate({opacity: 0}, 100 );
                   $("#imgScrollRight").animate({opacity: 1}, 100 );
                }
            } else {
                $("#imgScrollLeft").animate({opacity: 0}, 100 );
                $("#imgScrollRight").animate({opacity: 1}, 100 );
            }   
        }
    });
    
    $("#offersNavList li a").bind('click', function(){
    
          $(this).tabTrigger();
          return false;
    });
    
    //read cookie and set correct tab if exist
    
    var activeTab = readCookie('activeTab');
    
    //alert(activeTab);
    if(activeTab && activeTab != "undefined" && listItems > 0){
    
        activeTab = readCookie('activeTab');
        currentTab = $("#offersNavList li a[id="+activeTab+"]");
        currentTabIndex = linksList.index(currentTab) + 1;
        
        if((listItems > 7) && (currentTabIndex > 7)){
            //alert(currentTabIndex);
            scrollIndent = (currentTabIndex - 7)*123;
            $("#offersNav").animate({left: '-='+scrollIndent}, 500);
            itemCount = currentTabIndex - 7;
            
            if(itemCount == listItems - 7 ){
                $("#imgScrollLeft").animate({opacity: 1}, 100 );
                $("#imgScrollRight").animate({opacity: 0}, 100 );
            }
        }
        
        $("#offersNavList li a[id="+activeTab+"]").tabTrigger();
    }
    
    //set cookie if landing page offers link is clicked
    
    var offersLinksList = $(".homeOffersInner .offerLink");
    //alert(offersLinksList.size());
    
    if(offersLinksList = $(".homeOffersInner .offerLink")){
        
        offersLinksList.click(function(){
        
            thisOfferCategory = $(this).attr("rel");
            //alert(thisOfferCategory);
            createCookie('activeTab',thisOfferCategory,20);
        });
        
    };
});

// courtesy of quirksmode.org
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*60*1000)); //so it minutes now actually
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}



	
	
	

