var site_title = $("title").text();

$(function() {
    
        /* Home Page */
        
        
        
        // Button hover effect, and also change out main image
        $(".home_top_item").hover(function(){
            var cur_bg = $(this).css('background-image');
            
            $(this).css('background-image',cur_bg.replace('.png','_hover.png'));
            
            // Load image name based on ID
            $("#home_top_main").html('<img src="' + base  + 'assets/images/home/' + $(this).attr('id') + '.jpg" />');
            
        },function(){
            var cur_bg = $(this).css('background-image');
            $(this).css('background-image',cur_bg.replace('_hover.png','.png'));
        });
        
        // Follow links based on items ID
        $(".home_top_item").click(function(){
           
           var url = $(this).attr('id').replace(new RegExp("_", "g"), '/');
           window.location =  base + 'index.php/' + url;
        });
    
        
        // Go to film services page
        $("#home_filmservices").click(function(){
            window.location = base + 'index.php/page/film-and-hollywood-services';
        });
        
    
        /* Store */
	
	$("a[rel='itemcolorbox']").colorbox();
	
	$(".album_tracklisting_toggle").click(function(){
		var albumid = $(this).attr('id').replace('album_tracklisting_toggle_','');
		$("#album_tracklisting_"+albumid).toggle();
		return false;
	});
	
	/* Add item to shopping cart */
	$(".store_addtocart").click(function() {
		
		var item_id = $(this).attr('id');
		
		$.get($(this).attr('href'),function(data)
		{
			$("#"+item_id).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
		
			$(".store_shoppingcart_empty").fadeOut();
			$(".store_shoppingcart_items").html(data);
		});
		return false;
	});
	
	/* Alert before clearing cart */
	$("#store_empty_cart").click(function()
	{
		var ans = confirm("Empty all shopping cart items?");
		
		if(!ans){
			return false;
		}
	});
	
	/* Validate checkout form */
	$("#checkoutSubmit").click(function() {
		var error = false;
		
		$(".error").hide();
		
		if($("#firstname").val().length<2)
		{
			error = true;
			$("#firstnameError").show();
		}
		if($("#lastname").val().length<2)
		{
			error = true;
			$("#lastnameError").show();
		}
		if($("#address").val().length<2)
		{
			error = true;
			$("#addressError").show();
		}
		if($("#city").val().length<2)
		{
			error = true;
			$("#cityError").show();
		}
		if($("#zip").val().length<2)
		{
			error = true;
			$("#zipError").show();
		}
		if($("#phone").val().length<2)
		{
			error = true;
			$("#phoneError").show();
		}
		if($("#email").val().length<2)
		{
			error = true;
			$("#emailError").show();
		}
		
		if(error) return false;
	});	
	
	
	/* Change sorting order of store */
	$("#sortby").change(function(){
		var opt = $(this).val();
		
		insertParam('sortby',opt);
	});	
});


function insertParam(key, value)
{
    key = escape(key); value = escape(value);

    var kvp = document.location.search.substr(1).split('&');

    var i=kvp.length; var x; while(i--) 
    {
        x = kvp[i].split('=');

        if (x[0]==key)
        {
                x[1] = value;
                kvp[i] = x.join('=');
                break;
        }
    }

    if(i<0) {kvp[kvp.length] = [key,value].join('=');}

    //this will reload the page, it's likely better to store this until finished
    document.location.search = kvp.join('&'); 
}
