﻿$(document).ready(function() {

						   

	$('#slideshow').nivoSlider({

		controlNav: false,

		directionNav: false,

		pauseOnHover: false,

		effect:'random'

	});

	

	$('#tradeslideshow').nivoSlider({

		controlNav: false,

		directionNav: false,

		pauseOnHover: false,

		effect:'random'

	});

	

	 // contact form default text

	if ($(".contactname").val() == '') {

	 	$(".contactname").val("Your Name");	

	}

	

	$(".contactname").focus(function() {

		if ($(this).val() == "Your Name") {

			$(this).val("");	

		}

	 });

	

	$(".contactname").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Your Name");	

		}

	 });

	 

	if ($(".contactemail").val() == '') {

	 	$(".contactemail").val("Email address");	

	}

	

	$(".contactemail").focus(function() {

		if ($(this).val() == "Email address") {

			$(this).val("");	

		}

	 });

	

	$(".contactemail").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Email address");	

		}

	 });

	 

	if ($(".contactsubject").val() == '') {

	 	$(".contactsubject").val("Subject");	

	}

	

	$(".contactsubject").focus(function() {

		if ($(this).val() == "Subject") {

			$(this).val("");	

		}

	 });

	

	$(".contactsubject").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Subject");	

		}

	 });

	 

	if ($(".contactmessage").val() == '') {

	 	$(".contactmessage").val("Message");	

	}

	

	$(".contactmessage").focus(function() {

		if ($(this).val() == "Message") {

			$(this).val("");	

		}

	 });

	

	$(".contactmessage").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Message");	

		}

	 });

	

	 // submit contact form

	 $(".contactsubmit").click(function() {

		$("form.contact").submit();

		return false;

	 });

	 

	 $("form.contact").submit(function() {

		

		if ($(".contactname").val() != "Your name" && $(".contactrecipient").val() != "" && $(".contactmessage").val() != "Message") {

			

			var x=$('.contactemail').val();

			var atpos=x.indexOf("@");

			var dotpos=x.lastIndexOf(".");

			if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {

				$(".msg").text("Not a valid e-mail address").removeClass("success").addClass("error");

				return false;

			}

			

		} else {

			$(".msg").text("Please enter all the required information").removeClass("success").addClass("error");

			return false;

		}



	 });

	 

	 // resellers form default text

	if ($(".rname").val() == '') {

	 	$(".rname").val("Name");	

	}

	

	$(".rname").focus(function() {

		if ($(this).val() == "Name") {

			$(this).val("");	

		}

	 });

	

	$(".rname").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Name");	

		}

	 });

	 

	if ($(".rcompany").val() == '') {

	 	$(".rcompany").val("Company Name");	

	}

	

	$(".rcompany").focus(function() {

		if ($(this).val() == "Company Name") {

			$(this).val("");	

		}

	 });

	

	$(".rcompany").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Company Name");	

		}

	 });

	 

	if ($(".rshops").val() == '') {

	 	$(".rshops").val("No. of Shops");	

	}

	

	$(".rshops").focus(function() {

		if ($(this).val() == "No. of Shops") {

			$(this).val("");	

		}

	 });

	

	$(".rshops").blur(function() {

		if ($(this).val() == "") {

			$(this).val("No. of Shops");	

		}

	 });

	 

	 if ($(".rcity").val() == '') {

	 	$(".rcity").val("City");	

	}

	

	$(".rcity").focus(function() {

		if ($(this).val() == "City") {

			$(this).val("");	

		}

	 });

	

	$(".rcity").blur(function() {

		if ($(this).val() == "") {

			$(this).val("City");	

		}

	 });

	 

	  if ($(".remail").val() == '') {

	 	$(".remail").val("Email");	

	}

	

	$(".remail").focus(function() {

		if ($(this).val() == "Email") {

			$(this).val("");	

		}

	 });

	

	$(".remail").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Email");	

		}

	 });

	 

	  if ($(".rwebsite").val() == '') {

	 	$(".rwebsite").val("Website");	

	}

	

	$(".rwebsite").focus(function() {

		if ($(this).val() == "Website") {

			$(this).val("");	

		}

	 });

	

	$(".rwebsite").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Website");	

		}

	 });

	 

	 if ($(".rphone").val() == '') {

	 	$(".rphone").val("Phone");	

	}

	

	$(".rphone").focus(function() {

		if ($(this).val() == "Phone") {

			$(this).val("");	

		}

	 });

	

	$(".rphone").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Phone");	

		}

	 });

	 

	if ($(".rmessage").val() == '') {

	 	$(".rmessage").val("About You");	

	}

	

	$(".rmessage").focus(function() {

		if ($(this).val() == "About You") {

			$(this).val("");	

		}

	 });

	

	$(".rmessage").blur(function() {

		if ($(this).val() == "") {

			$(this).val("About You");	

		}

	 });

		

	 // submit contact form

	 $(".rsubmit").click(function() {

		$("form.resellers").submit();

		return false;

	 });

	 

	 $("form.resellers").submit(function() {

		

		if ($(".rname").val() != "Name" && $(".rcompany").val() != "Company Name" && $(".rtype").val() != "" && $(".rshops").val() != "No. of Shops" && $(".rcountry").val() != ""  && $(".remail").val() != "Email" && $(".rwebsite").val() != "Website" && $(".rphone").val() != "Phone" && $(".rmessage").val() != "About You" ) {

			

			var x=$('.remail').val();

			var atpos=x.indexOf("@");

			var dotpos=x.lastIndexOf(".");

			if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {

				$(".msg").text("Not a valid e-mail address").removeClass("success").addClass("error");

				return false;

			}

			

		} else {

			$(".msg").text("Please enter all the required information").removeClass("success").addClass("error");

			return false;

		}



	 });

	 

	 // login form

	$(".loginsubmit").click(function() {

		$("form.login").submit();

		return false;

	});

	

	$(".forgotsubmit").click(function() {

		$("form.forgot").submit();

		return false;

	});

	

	$("p.forgotpass a").click(function() {

		$("div.forgotpass").addClass("open");

		$(this).parent().remove()

		return false;

	});

	 

	

	

	// ie6

	if ($.browser.msie && $.browser.version.substr(0,1)<7) {

		$('body').addClass("ie6");

	}

	

	// ie7

	if ($.browser.msie && $.browser.version.substr(0,1)==7) {

		$('body').addClass("ie7");

	}

	

	// collection

	var items = $("#collectionleft li").size();

	var perPage = 10;

	var totalPages = Math.ceil(items / perPage);

	var currentPage = 1;

	var sliding = false;

	var timer = null;

	

	function controlsWidget() {

		$(".collectioncontrols .pg").html("<a href='#' class='prevpg'>&#9668;</a> Page " + currentPage + "/" + totalPages + " <a href='#' class='nextpg'>&#9658;</a>");

		$(".collectioncontrols .nn").html("<a href='#' class='prev'><span>&#9668;</span> Back</a> &nbsp;&nbsp;&nbsp;&nbsp; <a href='#' class='next'>Next <span>&#9658;</span></a>");

		if (sliding) {

			$(".collectioncontrols .slide").html("<a href='#' class='stop'>Pause slideshow <span>&#9612;&#9612;</span></a>");

		} else {

			$(".collectioncontrols .slide").html("<a href='#' class='play'>Play slideshow <span>&#9658;</span></a>");

		}

	}

	

	controlsWidget();

	

	// nav

	$(".collectioncontrols .next").live("click", function() {

		sliding = false;

		changeSlide("next");

		return false;

	});

	

	$(".collectioncontrols .prev").live("click", function() {

		sliding = false;

		changeSlide("prev");

		return false;

	});

	

	// pages

	$(".collectioncontrols .nextpg").live("click", function() {

		sliding = false;

		changeSlide("nextpg");

		return false;

	});

	

	$(".collectioncontrols .prevpg").live("click", function() {

		sliding = false;

		changeSlide("prevpg");

		return false;

	});

	

	$(".collectioncontrols .play").live("click", function() {

		sliding = true;

		changeSlide("next");

		return false;

	});

	

	$(".collectioncontrols .stop").live("click", function() {

		sliding = false;

		window.clearTimeout(timer);

		controlsWidget();

		return false;

	});

	

	// click

	$("#collectionleft li").click(function() {

		sliding = false;

		if ($(this).index() == "0") {

			changeSlide("initial");

		} else {

			changeSlide($(this).index());

		}

		

		return false;

	});

	

	changeSlide("initial");

	

	function changeSlide(mode) {

		controlsWidget();

	

		if (mode == "initial") {

			index = 0;

		} else if (mode == "next") {

			index = $("#collectionleft li.active").index() + 1;

		} else if (mode == "prev") {

			index = $("#collectionleft li.active").index() - 1;

		} else if (mode == "nextpg") {

			index = (currentPage)*perPage;

		} else if (mode == "prevpg") {

			index = (currentPage-2)*perPage;

			if (index < 0) {

				index = (totalPages-1) *perPage;

			}

		} else if (mode != '') {

			index = mode;

		}

		

		if (mode != "") {

				

			$("#collectionright .overlay").fadeIn(500, function() {

				$("#collectionleft li").removeClass("active");

				$("#collectionleft li:eq(" + index + ")")	.addClass("active");

				if (index < 0) {

					$("#collectionleft li:last").addClass("active");

				} else if  ($("#collectionleft li.active").size() == 0) {

					$("#collectionleft li:first").addClass("active");

				}

				$("#collectionright").css("background-image", "url('" + $("#collectionleft li.active a").attr("href") + "')");

				$(".collectioncaption").html($("#collectionleft li.active a").attr("title"));

				index = $("#collectionleft li.active").index();

				itemnumber = index + 1;

				currentPage = Math.ceil(itemnumber/perPage);

				controlsWidget();

				$("#collectionleft").scrollTop(480*(currentPage-1));

				$("#collectionright .overlay").fadeOut(500, function() {

			

				});

			});

			

			if (sliding) {

				timer = window.setTimeout(function() {

					changeSlide("next");

				}, 3000);

			}			

		}

	}

		

	// qr code

	$("a.qrcode").colorbox();

	

	// image previews in cart (no animations for ie8 and under)

	if ($('.catimg').size() > 0) {

		if ($.browser.msie && $.browser.version.substr(0,1)<9) {

			$('.catimg').imgPreview({

				containerID: 'imgPreviewWithStyles',

				srcAttr: 'rel',

				imgCSS: {

					// Limit preview size:

					height: 300

				}

			});

		} else {

			$('.catimg').imgPreview({

				containerID: 'imgPreviewWithStyles',

				srcAttr: 'rel',

				imgCSS: {

					// Limit preview size:

					height: 300

				},

				// When container is shown:

				onShow: function(link){

					// Animate link:

					$(link).stop().animate({opacity:0.4});

					// Reset image:

					$('img', this).stop().css({opacity:0});

				},

				// When image has loaded:

				onLoad: function(){

					// Animate image

					$(this).animate({opacity:1}, 300);

				},

				// When container hides: 

				onHide: function(link){

					// Animate link:

					$(link).stop().animate({opacity:1});

				}

			});

		}

	}

	

	

	// cart search

	if ($(".artno").val() == '') {

	 	$(".artno").val("Search");	

	}

	

	$(".artno").focus(function() {

		if ($(this).val() == "Search") {

			$(this).val("");	

		}

	 });

	

	$(".artno").blur(function() {

		if ($(this).val() == "") {

			$(this).val("Search");	

		}

	 });

	

	// submit cart search

	 $(".cartsearchsubmit").click(function() {

		$("form.cartsearch").submit();

		return false;

	 });

	 

	 // submit cart add

	 $(".cartaddsubmit").click(function() {

		$("form.cartadd").submit();

		return false;

	 });

	 

	 // submit cart checkout

	 $(".cartcheckoutsubmit").click(function() {

		$("form.cartcheckout").submit();

		return false;

	 });

	 

	  // submit cart remove

	 $(".cartremovesubmit").click(function() {

		$("form.cartremove").submit();

		return false;

	 });

	 

	// menu

	$("ul.nav > li").hover(function() {

		$("ul", this).slideDown();

	}, function() {

		$("ul", this).slideUp();

	});

});



window.onload = function() {

	 // map

	 if(typeof markers != 'undefined') {

		var latlng = new google.maps.LatLng(31.052934,16.52343);

		var myOptions = {

			zoom: 2,

			center: latlng,

			mapTypeId: google.maps.MapTypeId.ROADMAP,

			draggable:false,

			scrollwheel:false,

			streetViewControl:false,

			panControl:false,

			zoomControl:false,

			mapTypeControl:false,

			disableDoubleClickZoom:true

		};

		var icon = new google.maps.MarkerImage("assets/img/maps-pin.png");

				

		var map = new google.maps.Map(document.getElementById("map_canvas"),	myOptions);

		

		for (var m in markers) {

			var myLatLong = new google.maps.LatLng(markers[m].lat,markers[m].long)

			if (markers[m].type == "needed") {

				var marker = new google.maps.Marker({

					position: myLatLong, 

					map: map, 

					icon: icon,

					title:markers[m].title

				});

			} else if (markers[m].type == "resellers") {

				var marker = new google.maps.Marker({

					position: myLatLong, 

					map: map, 

					title:markers[m].title

				});

				

			}

			

			

			if (markers[m].type != "hidden") {

					

				var path = [

				  new google.maps.LatLng(35.0000, 105.0000),

				  myLatLong

				];

		

				var line = new google.maps.Polyline({

				  path: path,

				  strokeColor: '#000',

				  strokeOpacity: 0.4,

				  strokeWeight: 1,

				  geodesic: 0,

				  map: map,

				  clickable: 0

				});

			}

			

		}

		

	}

	

	 // contact map

	 if($('#contact_canvas').size() == 1) {

		var latlng = new google.maps.LatLng(23.194235313133035, 113.24209213256836);

		var myOptions = {

			zoom: 15,

			center: latlng,

			mapTypeId: google.maps.MapTypeId.ROADMAP

		};

		var map = new google.maps.Map(document.getElementById("contact_canvas"),	myOptions);

		

		var marker = new google.maps.Marker({

			position: latlng, 

			map: map, 

			title:'Y. Jessi'

		});

	}	

}
