/* Author: Brittney Kernan
URL: brittneykernan.com
*/

var firsthidden = 0;


function openStore() {

if( $('#store-container:hidden').length ) {
	$('#store-container').slideDown("",function() {
		$('#storeInfo,.tl,.tr,.bl,.br').fadeIn();
		//products width is set to enable scrolling
		$('#products').css("width", function() { 
			var width = 0;
			$('#products div').each( function() {
				width += $(this).width();	
			});  
			return width;
		});
	});
}
else if( $('#store-container:visible').length ) {
	$('#store-container').slideUp();
	$('#storeInfo,.tl,.tr,.bl,.br').fadeOut();	
}
}

function firstHide(which, num){
 firsthidden += num;
 if(firsthidden<1){
	hideIframe();
 }
}

function calcHeight(){
	 if(firsthidden>=1){
	 var the_height=document.getElementById('ZenCart').contentWindow.document.body.scrollHeight;
	 $('#ZenCart').css("height",the_height + 300);
	 $('#ZenCart').css("opacity",'0');
	 $('#ZenCart').css("visibility",'visible');
	 //$('#products').css("visibility",'hidden');
	  $('#ZenCart').animate({
	    opacity: 1
	  });
	 $('#products').fadeOut();
	 scroll(0,0);
	 }
}

function hideIframe(){
	 var the_height = 0;
	 $('#ZenCart').css("height",the_height);
	 //$('#ZenCart').css("visibility","hidden");
	 //$('#products').css("visibility","visible");
	 $('#ZenCart').animate({
	    opacity: 0
	  }, function() {
	  	 $('#ZenCart').css("visibility",'hidden');
	  });
	 $('#products').fadeIn();
}

function opacity(id, opacStart, opacEnd, millisec) {
	
	//speed for each frame
	var divid = 100;
	if(!window.innerHeight) { //ie 
		divid = 1000;
		$("#image").css('background-color', "#fff");
		$("#info").css('background-color',"#fff");
	}
	
	var speed = Math.round(millisec / divid);
	var timer = 0;

	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	$( '#' + id ).css('opacity', (opacity / 100) );
	//object.opacity = (opacity / 100);
	//object.filter = "alpha(opacity=" + opacity + ")";
} 

function openCart(url) {
	$(".productInfo").fadeOut();	
	$("#ZenCart").attr("src", url);
}

function itemClicked(element) {
	var price = $("#price");
	var name = $("#name");
	var buy = $("#buy");
	var productInfo = $(".productInfo");
	var soldout = $("#soldout");
	
	//$("#disabler").css("display","block");
	opacity('bagGrid',100,0,500);
	
	//productInfo.css( "display", "block";
	//$(productInfo).css("display","block");
	$("#image").attr( "src", $(element).attr("src") );
	var thisprice = $( '#' + $(element).attr("id") + '_price').val();
	
	if(thisprice == "Sold Out") {
		price.css( "display", "none");
		name.css( "display", "none");
		buy.css( "display", "none");
		soldout.css( "display", "block" );
	} else {
	 	//not sold out
		soldout.css( "display", "none" );
		price.css( "display", "block" );
		price.text( '$' + thisprice);
		name.text( $( '#' + $(element).attr("id") + '_name').val() );
		name.css( "display", "block" );
		buy.attr("href", "javascript:openCart('http://www.deeandricky.com/store/index.php?action=buy_now&products_id=" + $( '#' + $(element).attr("id") + '_id').val() + "')");
		buy.css( "display", "block" );
	}
	
	if(productInfo.offsetHeight > window.innerHeight) {
		$("#image").css('height',"300px") ;//- (productInfo.offsetHeight - window.innerHeight) + "px";
	}
	
	var infoWidth = productInfo.offsetWidth/2;
	var infoHeight = productInfo.offsetHeight/2;if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		prodWidth = window.innerWidth;
		prodHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		prodWidth = document.documentElement.clientWidth;
		prodHeight = document.documentElement.clientHeight;
	}
	
	var top = (prodHeight/2) - infoHeight;
	top = (top > 0) ? top + "px" : 0;
	
	productInfo.fadeIn();			
}

function goBack() {
	
	$(".productInfo").fadeOut();
	
}

//init
$(document).ready( function() {
	
				
	//add click feature
	$('#products img').click( function () {
			
		itemClicked($(this));
			
	});
	
	if( $('.handheld').css('display') != 'block' ) {
	 $("#products-container").mousewheel(function(event, delta) {

      this.scrollLeft -= (delta * 30);

      event.preventDefault();

   	});
   }
				
}); 

var loader = setTimeout( showLoader , 500 )   
   
$(window).load( function() {
	clearTimeout(loader);
   $('#loader').fadeOut('fast');
   $('#container').fadeIn();
   deeTwitter.start();
   rickyTwitter.start();
}); 

function showLoader () {
	$('#loader').fadeIn();
}












