// JavaScript Document

$(document).ready(function(){
						   
			var pageWidth = window.innerWidth;
			var	pageHeight = window.innerHeight;
			if (typeof pageWidth != 'number'){
				if (document.compatMode == 'CSS1Compat'){
					pageWidth = document.documentElement.clientWidth;
					pageHeight = document.documentElement.clientHeight;
				} else {
					pageWidth = document.body.clientWidth;
					pageHeight = document.body.clientHeight;
				}
			}
		
		var solaandy = Math.abs(Math.round((pageWidth - 840)/2));
		var pagesBg = Math.abs(Math.round((pageWidth - 1220)/2));
		$('#jquery').css({left:solaandy});
		$('#pages_bg').css({left:pagesBg});
		if(pageWidth > 1208){
			$('#pages_bg').css({width:'1208'});
		}else{
			$('#pages_bg').css({width:pageWidth});
		}
		if(pageWidth > 1200){
			$('#pages_bg').css({background:'url(/images/assets/bg.png)'});
		}
		
						   
	 $('#slider').nivoSlider();						   
						   
	
	$('#adminuser').focus();
	
	$('#addnewcat').bind('click',function(event){ $('#addcat').dialog({autoOpen:true,width:600,title:'Adebowale Electronics',modal:true,hide:'slide',show:'slide',buttons:{CLOSE:function(event) {$(this).dialog('close'); }}});
									   });
						   
						   
	//jquery for newsletter input
	
	$('#newsletter_body input[type=text]:first').focus(function(){
																if($(this).val() == '--enter phone number here--'){
																	$(this).val('');
																}
																});
	
	$('#newsletter_body input[type=text]:last').focus(function(){
															   if($(this).val() == '--enter email address here--'){
																$(this).val('');
															   }
															   });
	
	$('#newsletter_body input[type=text]:last').focusout(function(){
															  if($(this).val() == ''){
																  $(this).val('--enter email address here--');
															  }
															  });
	
	$('#newsletter_body input[type=text]:first').focusout(function(){
															  if($(this).val() == ''){
																  $(this).val('--enter phone number here--');
															  }
															  });
	
	
	//ajax starts here
						   
	$('.product_order a').live('click',function(event){
												$('#search_one').hide();
										
										$.ajax({
											   url: '/products/ajaxDetail',
											   data: {
												   'model':$(this).attr('id'),
												   'brand':$(this).attr('name')
												   },
												success: function(response){
													$('#actual_products').hide();
													$('#actual_products').html(response);
													$('#actual_products').show('slow');
													//alert(response);
													$('#search_next a').hide('slow');
													$('#search_prev a').hide('slow');
												}
											   });
												   
											
									
										});
	
	$('#cat_body a').live('click',function(event){
										   
										  								
										$.ajax({
											   url: '/products/fetchProducts',
											   data: {'category':$(this).attr('name')
												   },
												success: function(response){
													$('#actual_products').hide();
													$('#actual_products').html(response);
													$('#actual_products').slideDown(2000);
													//alert(response);
													$('#search_two').html(' ');
													$('#search_select>option:first').attr({'selected':'selected'});
													$('#search_one').show('slow');
												},
												complete: function(){
													var next = $('#ajax_next').attr('next');
													
													if(next !== undefined){
														
														var solaa = Number(next);
													
														if(solaa != 0){
															$('#search_next a').show('slow');
														}
													}else{
														$('#search_next a').hide('slow');
													}
												
													
													
												},
											
											   });
											var sola = $(this).attr('name');
											$('#search_select').attr({'name':sola});
											$('#search_prev a:first').hide('slow');
				
										});
	
	$('#ajax_loading').bind('ajaxStart', function(event){
												var andy = $('<span><img src=\'/images/assets/tino_loader.gif\' />Searching our achive...</span>');
												$(this).html(andy);
												});
	$('#ajax_loading').bind('ajaxComplete', function(event){
												
												$(this).html(' ');
												});
	
	
	$('#search_select').live('change',function(event){
											   if($(this).val() == 'brand'){
												  $('#search_next a').hide('slow');
												  $('#search_prev a:first').hide('slow');
												   $.ajax({
														  url:'/products/fetchBrands',
														  data:{'category':$(this).attr('name')},
														  success:function(response){
															  //alert(response);
															  
															  $('#search_two').html(response).show('slow');
														  }
														  });
												  
											   }else{
												   if($(this).val() == 'all'){
												   $.ajax({
														  url:'/products/fetchProducts',
														  data:{'category':$(this).attr('name'),
														  		},
														  success:function(response){
															  	$('#actual_products').hide();
																$('#actual_products').html(response);
																$('#actual_products').slideDown(2000);
															  //alert(response);
															  $('#search_two').html(' ');
														  },
														  complete: function(){
																var next = $('#ajax_next').attr('next');
																
																if(next !== undefined){
																	
																	var solaa = Number(next);
																
																	if(solaa != 0){
																		$('#search_next a').show('slow');
																	}
																}else{
																	$('#search_next a').hide('slow');
																}
															
															},
														  });
												   }
											   }
												   
											   
												 });
	$('#brand_searched').live('change',function(event){
											   if($(this).val() != ''){
												  $('#search_next a').hide('slow');
												  $.ajax({
														  url:'/products/fetchByBrands',
														  data:{'category':$(this).attr('name'),
														  		'brand':$(this).val()},
														  success:function(response){
															  //alert(response);
															  	$('#actual_products').hide();
																$('#actual_products').html(response);
																$('#actual_products').show('slow');
																
														  }
														  });
												
												  
											   }
											   
												 });
	
	$('#detail_navigate a').live('click',function(event){
												  $('#detail_navigate a').attr('class',' ');
												  $(this).attr('class','active');
												 
												 $.ajax({
														url:'/products/fetchDetails',
														data:{'param':$(this).attr('id'),
																'filters':$(this).attr('name')},
														success:function(response){
															$('#detail_text').html(response);
														}
														});
												  });
	
		$('#product_order a').live('click',function(event){
										
											$.ajax({
												   url:'/products/orderProduct',
												   data:{'brand':$(this).attr('id'),
												   			'model':$(this).attr('name')},
													success:function(response){
														
														$('#order').html(response);
													},
													complete:function(){
														$('#order').dialog({autoOpen:true,width:500,title:'Adebowale Electronics',modal:true,hide:'slide',show:'slide'});
									   
														
													}
												   });
												   
											
									
										});
		
		$('#search_next a').live('click',function(event){
										   
										  								
										$.ajax({
											   url: '/products/fetchPaginatedProducts',
											   data: {'category':$('#ajax_next').attr('category'),
											   			'total':$('#ajax_next').attr('total'),
														'anchor':$('#ajax_next').attr('prev')+'/'+$('#ajax_next').attr('next')
												   },
												success: function(response){
													$('#actual_products').hide();
													$('#actual_products').html(response);
													$('#actual_products').slideDown(2000).show();
													//alert(response);
													$('#search_two').html(' ');
													//$('#search_one').html('<select name="" id="search_select"><option value="all">All</option><option value="brand">By Brand</option></select>');
												},
												complete: function(){
													var next = $('#ajax_next').attr('next');
													var sola = Number(next);
													if(sola == 0){
														$('#search_next a').hide('slow');
													}
													
													
												},
											
											   });
											//var sola = $('#search_select').attr('name');
											
											//$('#search_select').attr({'name':sola});
											$('#search_prev a:first').show('slow');
				
										});
		
		$('#search_prev a:first').live('click',function(event){
										   
										  								
										$.ajax({
											   url: '/products/fetchPaginatedProducts',
											   data: {'category':$('#ajax_next').attr('category'),
											   			'total':$('#ajax_next').attr('total'),
														'anchor':$('#ajax_next').attr('prev')+'/'+$('#ajax_next').attr('next'),
														'previous':'yes',
												   },
												success: function(response){
													$('#actual_products').hide();
													$('#actual_products').html(response);
													$('#actual_products').slideDown(2000);
													//alert(response);
													$('#search_two').html(' ');
													//$('#search_one').html('<select name="" id="search_select"><option value="all">All</option><option value="brand">By Brand</option></select>');
												},
												complete: function(){
													var next = $('#ajax_next').attr('prev');
													var sola = Number(next);
													if(sola == -9){
														
														$('#search_prev a:first').hide('slow');
													}
													
													
												},
											
											   });
											//var sola = $(this).attr('name');
											//$('#search_select').attr({'name':sola});
											$('#search_next a').show('slow');
				
										});
		
		
		$('#edit-product').live('click', function(event){
												  $('#search_one').hide();
												  
												  $.ajax({
														 url:'/products/editProduct',
														 data:{'param':$(this).attr('name')},
														 success:function(response){
															 //alert(response);
															$('#actual_products').hide();
															$('#actual_products').html(response);
															$('#actual_products').slideDown(2000).show();
															 
														 }
														 });
												  });
		
		



});
