/*
---------------------------------------------------||
* @script name: mini scroller gallery
* @desc: sample description...
* @author: Leysam Bernard D. Rosario
* @version: 0.1
---------------------------------------------------||
*/

/* ----- Start of the script ----- */
// $(document).ready(function(){
$(window).load(function(){							  
	var $slider = $("#slider");
	var $vals = $(".cat_value");
	var $product_container = $('#product_container');
	var $gal_container = $('#gallery_container');
	var $products = $('.product_item');
	var current_item = 1;
	var current_product;
	var trig;
	var $group = $('.product_group');
	
	$slider.width($(window).width());
	$gal_container.width($(window).width());
	seperator_width = ($slider.width() / $group.length)-3.2;
	$product_container.width();
	//alert(seperator_width*$products.length + " is eqzual to "+ $slider.width());
	//alert(seperator_width);
	for(i=0;i<$group.length;i++){
		//max_value = parseInt(max_value)+parseInt($vals[i].value);
		if(i == $products.length-1){
			//create seperator inside the scroller ------------------------------
			$slider.append('<div class="slider_seperator" id="for'+i+'" style="width:'+(seperator_width+4)+'px; border-right:#000 solid 0px; float:left; height:10px;"></div>');
			//creation of the seperator inside the scroller ends here -----------
		}else{
			//create seperator inside the scroller ------------------------------
			$slider.append('<div class="slider_seperator" id="for'+i+'" style="width:'+seperator_width+'px; margin-right:3px; float:left; height:10px;"></div>');
			//creation of the seperator inside the scroller ends here -----------
		}
	};
	
	$('#product_container').css("display","block");
	$('.preload').css("display","none");
	slideto(1);
	
	$items = $('.slider_seperator');
	
	$($items[0]).attr('class','slider_seperator active_seperator');
	for(i=0;i<$items.length;i++){
		$($items[i]).click(function(){
			$items.attr('class','slider_seperator');
			$(this).attr('class','slider_seperator active_seperator');
			group_id = $(this).attr('id').replace(/for/gi,"");
			//$($group[group_id]).attr('id');
			p_num=0;
			for(c=0;c<group_id;c++){
				cItem = $($group[c]).children('div');
				for(x=0;x<cItem.length;x++){
					p_num = p_num+1;
				}
			}			
			if(p_num ==0){
				slideto(1);
			}else{
				slideto(p_num);
			}
		});
	};
$b=$('.item_desc');
for(i=0;i<$b.length;i++){
	$x = $($b[i]).parent();
	z = $x.children();
	w=$(z).width();
	$($b[i]).width(w).css({opacity:'0.7'});
}
function next(obj){
	my_id = $(obj).attr('id');
	my_id = my_id.replace(/image_for_/gi,"");
	current_product = my_id;	
	n_width = "";
	
	$items = $('.slider_seperator');
	cGroup = $($products[my_id]).parent().attr('id').replace(/group_/gi,"");
	//parent = $($products[my_id]).parent();
	//alert(cGroup);
	$items.attr('class','slider_seperator');
	$($items[cGroup]).attr('class','slider_seperator active_seperator');
	
	for(c=0;c<my_id;c++){
		n_width = parseInt($($products[c]).width()+n_width);
	}
	$products.css({opacity:1});
	
	img_width2 = $($products[my_id]).width();
	deduct_width = (($slider.width()-img_width2)/2)-20;
	
	
	$products.attr('class','product_item');
	$products.attr('id','');
	
	$($products[current_product]).attr('class','product_item active_image');
		
	$p_item = $($products[current_product-1]);
	$n_item = $($products[parseInt(current_product)+1]);
					
	$n_item.attr('class','product_item next_img');
	$n_item.attr('id','image_for_'+(parseInt(current_product)+1)+'');
	
	$p_item.attr('class','product_item prev_img');
	$p_item.attr('id','image_for_'+(current_product-1)+'');
	
	$gal_container
		.animate({
			scrollLeft: n_width-deduct_width+(700)
		},1500,function(){
			//animation complete!~
			$products
				.css({
					opacity: 0.3
				});
				
			$('.active_image')
				.css({
					opacity: 1
				});				
		}
	);
		
	$('.next_img').click(function(){
		//double check
		if($(this).attr('class') == 'product_item next_img'){
			next(this);
		}
	});//next_img end
	
	$('.prev_img').click(function(){
		//double check
		if($(this).attr('class') == 'product_item prev_img'){
			prev(this);
		}
	});//next_img end
	
};

function prev(obj){
	my_id = $(obj).attr('id');
	my_id = my_id.replace(/image_for_/gi,"");
	current_product = my_id;	
	n_width = "";
	
	$items = $('.slider_seperator');
	cGroup = $($products[my_id]).parent().attr('id').replace(/group_/gi,"");
	//alert(cGroup);
	$items.attr('class','slider_seperator');
	$($items[cGroup]).attr('class','slider_seperator active_seperator');
	
	for(c=0;c<my_id;c++){
		n_width = parseInt($($products[c]).width()+n_width);
		/*if(c+1 == for_product){
			y_width = parseInt(($($products[c]).width()/2)+y_width);
		}else{
			y_width = parseInt($($products[c]).width()+y_width);
		}*/
	}
	
	$products.css({opacity:1});
	
	img_width2 = $($products[my_id]).width();
	deduct_width = (($slider.width()-img_width2)/2)-20;
	
	
	$products.attr('class','product_item');
	$products.attr('id','');
	
		
	$p_item = $($products[current_product-1]);
	$n_item = $($products[parseInt(current_product)+1]);
					
	$($products[current_product]).attr('class','product_item active_image');
	$n_item.attr('class','product_item next_img');
	$n_item.attr('id','image_for_'+(parseInt(current_product)+1)+'');
	
	$p_item.attr('class','product_item prev_img');
	$p_item.attr('id','image_for_'+(current_product-1)+'');
	
	$gal_container
		.animate({
			scrollLeft: n_width-deduct_width+(700)
		},1500,function(){
			//animation complete!~
				
				
					$products
						.css({
							opacity: 0.3
						});
						
					$('.active_image')
						.css({
							opacity: 1
						});				
				
		}
	);
		
	$('.next_img').click(function(){
		//double check
		if($(this).attr('class') == 'product_item next_img'){
			next(this);
		}
	});//next_img end
	
	
	
	$('.prev_img').click(function(){
		//double check
		if($(this).attr('class') == 'product_item prev_img'){
			prev(this);
		}
	});//next_img end
	
};

function slideto(id){
	my_id = id;
	current_product = my_id;
	n_width = "";
	for(c=0;c<my_id;c++){
		n_width = parseInt($($products[c]).width()+n_width);
		/*if(c+1 == for_product){
			y_width = parseInt(($($products[c]).width()/2)+y_width);
		}else{
			y_width = parseInt($($products[c]).width()+y_width);
		}*/
	}
	
	$products.css({opacity:1});
	
	img_width2 = $($products[my_id]).width();
	deduct_width = (($slider.width()-img_width2)/2)-20;
	
	$products.attr('class','product_item');
	$products.attr('id','');
		
	$p_item = $($products[current_product-1]);
	$n_item = $($products[parseInt(current_product)+1]);
	
	$($products[current_product]).attr('class','product_item active_image');
					
	$n_item.attr('class','product_item next_img');
	$n_item.attr('id','image_for_'+(parseInt(current_product)+1)+'');
	
	$p_item.attr('class','product_item prev_img');
	$p_item.attr('id','image_for_'+(current_product-1)+'');
	
	$gal_container
		.animate({
			scrollLeft: n_width-deduct_width+(700)
		},1500,function(){
			//animation complete!~
				
					$products
						.css({
							opacity: 0.3
						});
						
					$('.active_image')
						.css({
							opacity: 1
						});				
		}
	);
		
	$('.next_img').click(function(){
		//double check
		if($(this).attr('class') == 'product_item next_img'){
			next(this);
		}
	});//next_img end
	
	$('.prev_img').click(function(){
		//double check
		if($(this).attr('class') == 'product_item prev_img'){
			prev(this);
		}
	});//next_img end
	
}


		
});

var timer;
var c_right=false;


$(document).ready(function(){
													 
$('.gal_thumb_img').mouseover(function(){
	$(this).animate({opacity:0.5});
});
													 
$('.gal_thumb_img').mouseout(function(){
	$(this).animate({opacity:1});
});
	
	
	$('.thumb_img').animate({
							scrollLeft:0
						},'fast');
	
	/*-start other script-*/
	
	$thumb_img_holder = $('#thumb_img');
	
	$gtm = $('.gal_thumb_img'); 
	
	$('.arrowRight').click(function(){
		$('.thumb_img')
			.animate({
				scrollLeft: "+="+660
			},2000,function(){
		});
	});
	$('.arrowLeft').click(function(){
		$('.thumb_img')
			.animate({
				scrollLeft: "-="+660
			},2000,function(){
		});
	});
	
	
	comWidth = $gtm.length*70;
	comWidth = (comWidth)+$gtm.length*4
	
	$('.img_thumb_holder').width(comWidth);
	
	
	$('.gal_thumb_img')
		.click(function(){
			$('.gal_huge_img').attr('src',$(this).attr('alt'));
			hidden_value = $(this).attr('id').replace(/myidis_/gi,"");
			$('#add_this_product').val(hidden_value);		
			$('.img_name_holder').html($(this).attr('title'));
			
			Cufon.replace('body', {
			hover: true
				});
		});
	
	
	$('.img_add_holder').click(function(){
		products_id = $('#add_this_product').val();
		product_name = $('.img_name_holder').html();
		
		
		$incart = $('.cart_hidden_values');
		check = 0;
		for(i=0;i<$incart.length;i++){
			if($($incart[i]).val() == products_id){
				check = check+1;
			}
		}
		
		if(check > 0){
			alert('This product is already in your cart');
		}else{
			
			$('.images_cart').append('<div class="product_incart_holder"><input type="hidden" class="cart_hidden_values" name="product_in_cart[]" value="'+products_id+'"><span>'+product_name+'</span><span class="close_my_parent">X</span></div>');
		Cufon.replace('body', {
			hover: true
			});
		
		}
		
		$('.close_my_parent').click(function(){
			$(this).parent('div').remove();
		});
		
		
	});
})

function if_added(id){
	$incart = $('.cart_hidden_values');
	check = 0;
	for(i=0;i<$incart.lenth;i++){
		if($($incart[i]).val() == id){
			check = check+1;
		}
	}
	
	if(check > 0){
		return true;
	}else{
		return false;
	}
}
//Script end's here
