// Theme Slider
var previousThemeId;
var themeSlideSpeed = 200;
var sitesDir = '/sites/all/themes/conrad/';

$(document).ready(function(){
	// Theme image persist
    if($.cookie("theme")){setThemeImages($.cookie("theme"))}
    else{setThemeImages("")};
    
    // Theme selector color persist
    if($.cookie("theme")){themeSel($("#dynamic #"+$.cookie("theme")))};
	
    $("#themeExpand").click(function(){slideThemes()});
    $("#themeExpand").mouseover(function(){slideThemes()});
    $("#themeSlider").mouseleave(function(){slideReset()});
    $("#themeSlider #dynamic li").click(function(){changeTheme($(this),true);slideReset();themeSel($(this));});
    $("#themeSlider #dynamic li").mouseover(function(){changeTheme($(this),false)});
    $("#themeSlider #dynamic li").mouseout(function(){changeTheme($("#"+previousThemeId),false)});
});

function nextTheme(step)
{	
	var curTheme = getCurrentThemeId();
	var next; 
	if (step>0){ 
		next = $("#"+curTheme).next();
		if (next.length == 0) {next =  $("#dynamic li").first() }
	}
	if (step<0){ 
		next = $("#"+curTheme).prev();
		if (next.length == 0) {next =  $("#dynamic li").last() }
	}
	changeTheme(next,true);
}

function themeSel(theme){
	if($(theme).attr("id") != 'random'){
		var themeColour = $(theme).children().css("background-color");
		if($.cookie("random")=="true"){
			$("#static #default span").css({
				"background-color": "#FFF",
				"color": "#3E5356"
			});
			$("#static #default span").html("?");
		}
		else{
			$("#static #default span").css({
				"background-color": themeColour,
				"color": themeColour
			});
		}
	}
	else{		
		$("#static #default span").css({
			"background-color": "#FFF",
			"color": "#3E5356"
		});
	}	
}

function getCurrentThemeId()
{
	var t;
    $("link").each(function(index){
    	 if($(this).attr("href").indexOf("css/themes") > -1){
    		t = $(this).attr("href").replace("/sites/all/themes/conrad/assets/css/themes/","").replace(".css","");
    	 }
    });
    return t;
}

function slideThemes(){
    $("#themeSlider #static").css("padding-bottom","6px");
    $("#themeSlider #dynamic").slideDown("fast");
}

function slideReset(){
    $("#themeSlider #dynamic").slideUp("fast",function(){
        $("#themeSlider #static").removeAttr("style");
    });
}

function changeTheme(theme,save){    
    var themeType = $(theme).attr("id");
    previousThemeId = getCurrentThemeId();
    if(themeType != "random"){
	    // Change Images
	    $(".goBtns").attr("src",sitesDir+"assets/img/themes/"+themeType+"/goButn.png");
	    $("#awardsImg").attr("src",sitesDir+"assets/img/themes/"+themeType+"/bottomLogos.jpg");
	    $("#slideLeft").attr("src",sitesDir+"assets/img/themes/"+themeType+"/panelLeft.jpg");
	    $("#slideRight").attr("src",sitesDir+"assets/img/themes/"+themeType+"/panelRight.jpg");
	    $("#custGoButn").attr("src",sitesDir+"assets/img/themes/"+themeType+"/goButn.png");
	    $("#searchGo").attr("src",sitesDir+"assets/img/themes/"+themeType+"/goButn.png");
	    $("#edit-submit").attr("src",sitesDir+"assets/img/themes/"+themeType+"/goButn.png");
	    $(".column-b .couponsSmall .coupon img").attr("src",sitesDir+"assets/img/themes/"+themeType+"/defaultCoupon.gif");
	    
	    // Fix for Cufon
	    var newColour = themeColour(themeType);
	    var newNavColour = themeNavColour(themeType); 
	    
	    Cufon.replace('#navigation li a', {
	        fontFamily : 'conMed',
	        fontSize: '19px',
	        color: newColour,
	        hover: {
					color: '#848484'
			}
	    	
	    });
	    Cufon.replace('h1',{
	        fontFamily : 'conMed',
	        color: newColour,
	        fontSize: '40px',
	    });
	    /*Cufon.replace('p.intro',{
	        fontFamily : 'Tahoma',
	        lineHeight : '2.4em',
	        color: newColour
	    });*/
	    Cufon.replace('#navigation li.active a',{
	        fontFamily : 'conMed',
	        fontSize: '19px',
	        color: "#000"
	    });
	    /*Cufon.replace('.couponsLarge a',{
	        fontFamily : 'Tahoma',
	        fontSize: '22px',
	        color: newNavColour
	    });*/
	    
	    
	    $("link").each(function(index){
	    	 if($(this).attr("href").indexOf("css/themes") > -1){
	    		 $(this).attr("href",sitesDir+"assets/css/themes/"+themeType+".css?1=2");
	    	 }
	    });
	   
	    // Create/Update cookie
	    if (save == true) {
	    	$.cookie("theme",themeType,{expires: 7,path: '/'})
	    	$.cookie("random",null,{path:"/",expires:7});
	    };
    }
    else if(themeType == "random" && save == true){    	
    	themeType = randomTheme();    	
    	if(themeType != getCurrentThemeId()){
    		$.cookie("theme",themeType,{path:"/",expires:7});
    		$.cookie("random","true",{path:"/",expires:7});
    		window.location.href = window.location.href;
    	}
    	else{changeTheme($("#dynamic #random"),true)};
    }
}

function themeNavColour(colour)
{
	 var newColour = "";
	    switch(colour){
	        case "pink":
	            newColour = "#f1b7cc"
	            break;
	        case "yellow":
	            newColour = "#f2e4b9"
	            break;
	        case "cerulean":
	            newColour = "#b7e1f1"
	            break;
	        case "viridian":
	            newColour = "#b7f1d5"
	            break;
	        case "cobalt":
	            newColour = "#443aa8"
	            break;
	        case "sienna":
	            newColour = "#efcfbb"
	            break; 
	        case "highcontrast":
	            newColour = "#f0558c"
	            break;              
	    }    
	    return newColour;
}

function themeColour(colour){
    var newColour = "";
    switch(colour){
        case "pink":
            newColour = "#CB1F5C"
            break;
        case "yellow":
            newColour = "#C89601"
            break;
        case "cerulean":
            newColour = "#00ADEF"
            break;
        case "viridian":
            newColour = "#0DB263"
            break;
        case "cobalt":
            newColour = "#443aa8"
            break;
        case "sienna":
            newColour = "#EC5800"
            break; 
        case "highcontrast":
            newColour = "#CB1F5C"
            break;              
    }    
    return newColour;
}

function setThemeImages(themeName){
	if(themeName != ""){
		// Set slider panel images and display
		$("#slideLeft").attr("src",sitesDir+"assets/img/themes/"+themeName+"/panelLeft.jpg");		
	    $("#slideRight").attr("src",sitesDir+"assets/img/themes/"+themeName+"/panelRight.jpg");
	    $("#slideLeft").show();
	    $("#slideRight").show();
	    
	    // Set go buttons
	    $("#searchGo").attr("src",sitesDir+"assets/img/themes/"+themeName+"/goButn.png");
	    $("#custGoButn").attr("src",sitesDir+"assets/img/themes/"+themeName+"/goButn.png");
	    $("#custGoButn").show();
	    
	    // Set miniCoupon images
	    $(".column-b .couponsSmall .coupon img").attr("src",sitesDir+"assets/img/themes/"+themeName+"/defaultCoupon.gif");
	}
	jQuery(function($) {
		$("#searchGo").pngfix();
	});
}

function randomTheme(){	
	var themes = ["pink","yellow","cerulean","viridian","cobalt","sienna"];
	var theme = themes[Math.floor(Math.random()*themes.length)];
	return theme;
}
