	function addCommas(nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}


function preload()
	{
		var args = new Array(
						 'public/custom/images/brands/alien-active.png',
						 'public/custom/images/brands/alien-inactive.png',
						 
						 'public/custom/images/brands/analog-active.png',
						 'public/custom/images/brands/analog-inactive.png',
						 
						 'public/custom/images/navs/contact-nav-active.png',
						 'public/custom/images/navs/contact-nav-inactive.png',
						 'public/custom/images/navs/home-nav-active.png',
						 'public/custom/images/navs/home-nav-inactive.png',
						 'public/custom/images/navs/links-nav-active.png',
						 'public/custom/images/navs/links-nav-inactive.png',
						 'public/custom/images/navs/photos-nav-active.png',
						 'public/custom/images/navs/photos-nav-inactive.png',
						 'public/custom/images/navs/team-nav-active.png',
						 'public/custom/images/navs/team-nav-inactive.png',
						 'public/custom/images/navs/thecompany-nav-active.png',
						 'public/custom/images/navs/thecompany-nav-inactive.png',
						 'public/custom/images/navs/shop-online-nav-active.png',
						 'public/custom/images/navs/shop-online-nav-inactive.png'
						 );

		document.arrArrayOfImages = new Array(args.length);

		for (var i=0; i<args.length; i++)
		{
			document.arrArrayOfImages[i] = new Image;
			document.arrArrayOfImages[i].src = 'http://www.circle8shop.com/' + args[i];
		}

	}
	
	preload();
	
	
	$(document).ready(function(){

						// online shopping ---- search tabs
			$("select.shop_search").change(function(){
 				window.location = BASE_URL + 'shop_online/' + $(this).attr("name") + '/' + this.value;
			});
	
			/* playing with images for products here */
			$(".product_details_photos").click(function(){

				$("#product_details_container").css('background','url('+ $(this).attr('src').replace(/_small/g,'_large') + ') no-repeat');
				$(".product_details_thumb_image").css('background','url('+ $(this).attr('src').replace(/_small/g,'_thumb') + ') no-repeat');
				$(".product_details_thumb_image").css('backgroundPosition','right 20px');
				
			});
			
			
			
			
			// online shopping ---- product details page
			$(".related_product").click(function(e){
				
				e.preventDefault();
				
				var link = $(this).attr('href');
			
				// update the images
				$("#product_details_container").css('background','url('+ $(this).children().find('.related_img').attr('src').replace(/_small/g,'_large') + ') no-repeat');
				$(".product_details_thumb_image").css('background','url('+ $(this).children().find('.related_img').attr('src').replace(/_small/g,'_thumb') + ') no-repeat');
				$(".product_details_thumb_image").css('backgroundPosition','right 20px');
				
				
				// update the useful informations
				$.getJSON(link, function(return_val){
					$('#item_brand_image').attr('src', return_val[0].image_brand);
					$('#item_name').html(return_val[1].item_name);
					
					var amount = new Number(return_val[2].item_price);
					$("#item_price").html("AED " + addCommas(amount.toFixed(2)));
					
					$("#item_availability").html(return_val[5].item_availability);
					
					$("#item_color_name").html(return_val[3].item_color);
					
					$("#item_size").html(return_val[4].item_size);
					
					$('#shop_online_buynow').attr('href', return_val[6].item_url);
					
					$('#prodDescription').html(return_val[7].item_description);
					
					
					if (return_val[5].item_availability == "Not Available"){
						$('#shop_online_buynow').css('display', 'none');	
					} else{
						$('#shop_online_buynow').css('display', 'block');	
					}
					
				});
				
			});
			
								
				
				// online shopping ---- product details page
				$("#imgProdDescription").click(function (e){
					e.preventDefault();
					
					$("#prodShippingInfo").css('display','none');
					$("#prodDescription").css('display','block');
					
					$(this).attr('src',$(this).attr("src").replace(/\binactive/g,'active'));
					$("#imgProdShippingInfo").attr('src',$("#imgProdShippingInfo").attr("src").replace(/\bactive/g,'inactive'));
					
				});
				
				$("#imgProdShippingInfo").click(function (e){
					e.preventDefault();

					$("#prodShippingInfo").css('display','block');
                    $("#prodDescription").css('display','none');

					$(this).attr('src',$(this).attr("src").replace(/\binactive/g,'active'));
					$("#imgProdDescription").attr('src',$("#imgProdDescription").attr("src").replace(/\bactive/g,'inactive'));

				});
				
				$("#magnify").click(function(){

    			match = $(".#product_details_container").css('background-image');
    			
				backurl = match.replace(/url\(/g,'');
				backurl = backurl.replace(/\)/g,'');
				backurl = backurl.replace(/\"/g,'');
				backurl = backurl.replace(/_large/g,'');
				
				$(this).attr('href', backurl);
    			

  			 });
  			 
 
				
				
			 // public ajax calls -- shopping online
  			 $("a#shop_online_buynow").click(function(e){
  			 	
  			 						e.preventDefault();	 
  			 	
  			 							var link = $(this).attr('href');
  			 							
										 $.getJSON(link, function(return_val){
											
											if ((typeof(return_val[2].global_cart_amount)!="undefined") && (return_val[2].global_cart_amount!= null))
												{
													var amount = new Number(return_val[2].global_cart_amount);
													$("#global_cart_amount").html("AED " + addCommas(amount.toFixed(2)));
													
													
												}
											if ((typeof(return_val[1].global_cart_count))!='undefined' && (return_val[1].global_cart_count != null)){
												$("#global_cart_item_count").html(" " + return_val[1].global_cart_count);
											} 
											

									    }); // end json
									    
									    
										// $(this).css('display','none');
										
										// TODO:
										// check if stocks: cart count for the item - stocks, if negative hide
										// update availability infos
										
								});
								

			$("#shop_online_view_cart").click( function(e) {
					
					e.preventDefault();
					
					jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) {
					    jAlert('Confirmed: ' + r, 'Confirmation Results');
					});

				});
				
			
			
			// get the number of li's tag for the jCarouselLite
			var ct = $('#slide_new_arrival > ul').children().size(); 
			if (ct > 5){
				$("#slide_new_arrival").jCarouselLite({
					
					auto: 800,
					speed: 6000
					
					
				});
			}


			

	});
	
	
	
	

		
		$(document).ready(function(){


				function doSomething() {

					//browser window is small
       				$("#mainbox").css('width',1684);
       				
					$("#leftside").css('width',442);
					$("#leftside .inner").css('width',442);
					$("#leftside .inner .logo").css('width',$("#leftside .inner").width());
		            $("#leftside .inner").css('backgroundPosition', '20px 430px');
		            $("#leftside .inner .logo div").css('backgroundPosition','');
		            $("#leftside .inner .logo div").css('width',299);
		            $("#leftside .inner .logo div").css('height',245);

					$("#rightside").css('width',442);
				  	$("#rightside .inner").css('width',442);
				  	$("#rightside .inner").css('backgroundPosition', '0px 430px');

					if ($(window).width() < 1300)
		                {
							//browser window is small
		                    $("#mainbox").css('width',1300);

		                    //adjust all left side width(s)
		              		$("#leftside").css('width',$("#leftside").width() - 150);
		  					$("#leftside .inner").css('width',$("#leftside .inner").width() - 150);
		  					$("#leftside .inner .logo").css('width',$("#leftside .inner").width());
		  					$("#leftside .inner .logo div").css('backgroundPosition','-7px 0');
				            $("#leftside .inner .logo div").css('width','100%');
				            $("#leftside .inner .logo div").css('height','100%');
		  					$("#leftside .inner").css('backgroundPosition', '-130px 430px');

		                    //adjust all right side width(s)
		              		$("#rightside").css('width',$("#rightside").width() - 250);
		  					$("#rightside .inner").css('width',$("#rightside .inner").width() - 240);
		  					$("#rightside .inner").css('backgroundPosition', '0px 430px');

					};
				}


				/*****************************************************************************************/
				/*****************************************************************************************/
				/* this area handles the main javascripted layout of the public frontend view of circle8 */
				/*****************************************************************************************/
				/*****************************************************************************************/
				var resizeTimer = null;

				$(window).bind('resize', function() {
				    if (resizeTimer) clearTimeout(resizeTimer);
				    resizeTimer = setTimeout(doSomething, 100);
				});
				
				//set minimum height for the mainbox
				$("#mainbox").css('min-height',$(document).height());

				//set height for the contents
				$("#contents").css('height',$("#contentmain").height() - 245);
				$("#contents").css('min-height',$("#contentmain").height() - 245);
				

				//put the hena background at the bottom of the page
				$("#henabg").css('background', "url(" + BASE_URL + "public/custom/images/bg-hena.jpg) -1px " + ( $("#contents").height() - 460).toString() + "px no-repeat");


				doSomething();

				//left side adjustment adustment
				$("#leftside").css('height', $("#mainbox").height());
				$("#leftside").css('background', "url(" + BASE_URL + "public/custom/images/bg-hena-left.jpg) " + ($("#leftside").width() - 30).toString() + "px " + ($("#leftside").height() - 224).toString() + "px no-repeat");

				//right side height adjustment
				$("#rightside").css('height', $("#mainbox").height());
				$("#rightside").css('background', "url(" + BASE_URL + "public/custom/images/bg-hena-right.jpg) 0px " + ($("#leftside").height() - 198).toString() + "px no-repeat");


				/*****************************************************************************************/
				/*****************************************************************************************/
				/* end																					 */
				/*****************************************************************************************/
				/*****************************************************************************************/



				/* navigation roll-overs */
				$(".mainnav, .navbrands").hover(function(){
    					  $(this).attr('src',$(this).attr("src").replace(/inactive/g,'active'));
					},
					function(){
					$(this).attr('src',$(this).attr("src").replace(/active/g,'inactive'));
						}

				);
				
				/* navigation roll-overs */
				$(".main_nav").hover(function(){
						
						$(this).css('backgroundPosition', '100% 0%');
				},function(){
						$(this).css('backgroundPosition', '0% 0%');
				});

				
				
				/* submit roll-overs */
				$("#submit").hover(function(){
						$(this).attr('style',$(this).attr("style").replace(/inactive/g,'active'));
					},
					function(){
					$(this).attr('style',$(this).attr("style").replace(/active/g,'inactive'));
						}

				);
				
				//dynamic tree
				$("#tree").dynatree({
										title: "Blog",
										rootVisible: false,
										persist: true,
							         	onSelect: function(dtnode) {
																		//alert ("You selected " + dtnode);
																		
																		if (dtnode.data.url){
																			window.location=dtnode.data.url;
																		}
																	}
									});

				/* $('#slide_holder').agile_carousel({
					phpURL: BASE_URL + "make_slides",
					no_transition_effect: 'no_transition_effect',
                    number_slides_visible: "1",
					first_last_buttons: "no",
					hover_next_prev_buttons: "no",
					next_prev_buttons: "no",
					pause_button: "no",
					slide_buttons: "yes",
					slide_captions: "|||Carousel That's Built to Order",
					slide_directory: "public/custom/images/ad",
					slide_links: ",,,,",
					slide_number_display: "no",
					timer_length: 7000,
					transition_duration: 2000,
					transition_easing: "easeInOutBack",
				    transition_type: "fade",
					water_mark: "no"
					 }); */
					 
					 
					 //alternative to slide holder - just an image refresh
					 
					 image_refresh();
				
				function image_refresh(){
					$.getJSON(BASE_URL + 'make_slides/make_single_image_refresh', function(return_val){
								
								
								
								if (return_val.length > 0){
									
									

										var p = return_val.length;
										var preBuffer = new Array()

										//prebuffering images
										for ( var i in return_val)
										{
											preBuffer[i] = new Image()
											preBuffer[i].src = return_val[i].filename
										} 
									
									
										var whichImage = Math.round(Math.random()*(p-1));

										$("#slide_holder").css('background', 'url(' + return_val[whichImage].filename + ') no-repeat')
										$("#slide_holder").children('img').css('display', 'none')
										
								}
											

						
					}); // end json										
									
			}


            /* playing with images for team here */
			$(".team_details_photos").click(function(){
				$("#team_details_container").css('background','url('+ $(this).attr('src').replace(/_thumb/g,'_big') + ') no-repeat');
			});
			
			$("#magnify-image").click(function(){

    			match = $("#team_details_container").css('background-image');

				backurl = match.replace(/url\(/g,'');
				backurl = backurl.replace(/\)/g,'');
				backurl = backurl.replace(/\"/g,'');
				backurl = backurl.replace(/_big/g,'_hires');

				$(this).attr('href', backurl);


  			 });





            /* playing with images for shops here */
			$(".shops_photos1").click(function(){
				$("#shop_container1").css('background','url('+ $(this).attr('src').replace(/_thumb/g,'_big') + ') no-repeat');
			});

			$("#shop-magnify-image1").click(function(){

    			match = $("#shop_container1").css('background-image');

				backurl = match.replace(/url\(/g,'');
				backurl = backurl.replace(/\)/g,'');
				backurl = backurl.replace(/\"/g,'');
				backurl = backurl.replace(/_big/g,'_hires');

				$(this).attr('href', backurl);


  			 });

            /* playing with images for shops here */
			$(".shops_photos2").click(function(){
				$("#shop_container2").css('background','url('+ $(this).attr('src').replace(/_thumb/g,'_big') + ') no-repeat');
			});


			$("#shop-magnify-image2").click(function(){

    			match = $("#shop_container2").css('background-image');

				backurl = match.replace(/url\(/g,'');
				backurl = backurl.replace(/\)/g,'');
				backurl = backurl.replace(/\"/g,'');
				backurl = backurl.replace(/_big/g,'_hires');

				$(this).attr('href', backurl);


  			 });


            /* playing with images for shops here */
			$(".shops_photos3").click(function(){
				$("#shop_container3").css('background','url('+ $(this).attr('src').replace(/_thumb/g,'_big') + ') no-repeat');
			});


			$("#shop-magnify-image3").click(function(){

    			match = $("#shop_container3").css('background-image');

				backurl = match.replace(/url\(/g,'');
				backurl = backurl.replace(/\)/g,'');
				backurl = backurl.replace(/\"/g,'');
				backurl = backurl.replace(/_big/g,'_hires');

				// alert(backurl);

				$(this).attr('href', backurl);


  			 });
			


		});


