var timr = null;
var timer = null;
$(function(){
	var xml = '/v/vspfiles/templates/vapparel/collection.xml';
	$.get(xml, function(dt){
		var Data = dt;
		$(Data).find("SAVED_EXPORT").each(function(i,d){
			var categoryname = $(d).find("categoryname").text();
			var url = $(d).find("url").text();
			$("#collection").append($("<option></option>").attr("value",url).text(categoryname));
		});
	});
	$(".jump").each(function(){
		var ref = $(this);
		$(this).next().click(function(){
			window.location=ref.val();
		});
	});

	// form validation
	$("input.verify").focus(function(){
		if($.trim($(this).val()) == $(this).attr("title"))
			$(this).val('');
	}).blur(function(){
		if($.trim($(this).val()) == '')
			$(this).val($(this).attr("title"));
	});
	$("input.chk").click(function(){
		var val = $.trim($(this).prev().val());
		var title = $(this).prev().attr("title");
		if(val == title || val == ''){
			$(this).prev().val('').focus();
			return false;
		} else if($(this).prev().hasClass("email")){
			return isValidEmail(val);
		}
		return true;
	});

	
	// for all a tag and img submit btn
	$("a, .chk").focus(function(){$(this).blur();});
	
	function onAfter(curr, next, opts){
	$("#count-shadow").text(opts.currSlide+1 + '/' + opts.slideCount);
}
function isValidEmail(e){
	var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/
	if(!(RegExp.test(e))){
		alert("A valid email address is required");
		return false;
	} else return true;
}

	// main promo slide show
	$('#slides').append('<div class="slide"> <div class="slide-text"><br /></div> <a href="http://www.plannercongresos.com/?page_id=166"><img src="http://www.plannercongresos.com/wp-content/themes/tabula-rosa/gallery/principal/Planner-20-web.jpg" width="543" height="350" alt="" /></a> <div class="slide-title"></div> </div><div class="slide"> <div class="slide-text"><br /></div> <a href="http://www.plannercongresos.com/?page_id=166"> <img src="http://www.plannercongresos.com/wp-content/themes/tabula-rosa/gallery/principal/Planner-20-web.jpg" width="543" height="350" alt="" /></a> <div class="slide-title"> </div> </div> <div class="slide"> <div class="slide-text"><br /></div> <a href="http://www.plannercongresos.com/?page_id=164"> <img src="http://www.plannercongresos.com/wp-content/themes/tabula-rosa/gallery/principal/Planner-20-web.jpg" width="543" height="350" alt="" /></a> <div class="slide-title"> </div> </div> <div class="slide"> <div class="slide-text"><br /></div> <a href="http://www.plannercongresos.com/?page_id=166"> <img src="http://www.plannercongresos.com/wp-content/themes/tabula-rosa/gallery/principal/Planner-20-web.jpg" width="543" height="350" alt="" /></a> <div class="slide-title"></div> </div> <div class="slide"> <div class="slide-text"><br /></div> <a href="http://www.plannercongresos.com/?page_id=164"> <img src="http://www.plannercongresos.com/wp-content/themes/tabula-rosa/gallery/principal/Planner-20-web.jpg" width="543" height="350" alt="" /></a> <div class="slide-title"></div> </div><div class="slide"> <div class="slide-text"><br /></div> <a href="http://www.plannercongresos.com/?page_id=164"> <img src="http://www.plannercongresos.com/wp-content/themes/tabula-rosa/gallery/principal/Planner-20-web.jpg" width="543" height="350" alt="" /></a> <div class="slide-title"></div> </div>  <div class="slide"> <div class="slide-text"><br /></div> <a href="http://www.plannercongresos.com/?page_id=166"><img src="http://www.plannercongresos.com/wp-content/themes/tabula-rosa/gallery/principal/Planner-20-web.jpg" width="543" height="350" alt="" /></a> <div class="slide-title"></div> </div>').cycle({
		prev:   '#prev', 
		next:   '#next',
		pause:	1,
		timeout:5000,
		after: onAfter

	});
	// play/pause of main promo
	$("#promo #pause").toggle(function(){$('#slides').cycle('pause');$(this).attr("src","http://www.plannercongresos.com/wp-content/themes/tabula-rosa/images/paused.png");},function(){$('#slides').cycle('resume');$(this).attr("src","http://www.plannercongresos.com/wp-content/themes/tabula-rosa/images/pause.png");});

	// text cycle
	$("#promo-texts, #promo-texts-middle").cycle({timeout:7000});
	$("#testimonials").cycle({timeout:0,random:1});
	// carousel slider
	// startCar
	var mycarousel_itemList = new Array();
	function mycarousel_initCallback(carousel){
		jQuery('#right-arrow').bind('click', function() {
			carousel.next();
			return false;
		});
		jQuery('#left-arrow').bind('click', function() {
			carousel.prev();
			return false;
		});
		
		carousel.clip.hover(function(){
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});

		$("ul#carousels li a > img ~ span.pop").live("mouseover", function(){
			clearTimeout(timr);
			carousel.stopAuto(); // pause the slider when the mouse is on span
		}).live("mouseout", function(){
			clearTimeout(timr);
			var ref = $(this);
			timr = setTimeout(function(){ref.stop().animate({top:'154px'});},1);
			carousel.startAuto();
		});
	}
	function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt){
		// The index() method calculates the index from a
		// given index who is out of the actual item range.
		var idx = carousel.index(i, mycarousel_itemList.length);
		carousel.add(i, mycarousel_getItemHTML(idx - 1));
	}
	function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt){
		carousel.remove(i);
	}
	function mycarousel_getItemHTML(item){
		return mycarousel_itemList[item];
	}
	function startCar(){
		$('#carousels').jcarousel({
			wrap: 'circular',
			scroll: 1,
			auto: 10,
			initCallback: mycarousel_initCallback,
			itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
			itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
		});
	}

function format(val){
	var ret = val.toString();
	if(ret.indexOf('.')==-1){
		return ret+'.00';
	}
	else{
		var total = 2 - (ret.length - (ret.indexOf('.') + 1));
		for(var i=0; i<total; i++) 
			ret += '0';
		return ret;
	}
}

	
	function Round(val, d){
	var ret = null;
	if(!d) ret = Math.round(val);
	
	if(typeof(d) == 'number'){
		var tmp = Math.pow(10,parseInt(d));
		ret = (Math.round(val*tmp))/tmp;
	}
	else{
		ret = Math.round(val);
	}
	return ret;
}

	$.get("/net/webservice.aspx",{api_name:'generic\\featured_products'},
		function(d){
			$('Product',d).each(function(i,p){
				var pu = $('PhotoURL',p).text().replace(/-2t./i,"-1.");
				mycarousel_itemList[i] = '<a href="/product-p/'+$('ProductCode',p).text()+'.htm"><img src="'+pu+'" width="150" height="150" alt="'+$('ProductName',p).text()+'" /><span class="pop"> '+$('ProductName',p).text()+' <span class="star"><img src="blank.gif" width="70" height="15" style="width:70px;height:15px" alt="star" /></span>          <div class="des">  <strong>Sale Price: $'+ format(Round($('ProductPrice',p).text(),2))+'</strong></div>          </span></a>';
			});
			$("#carousels").empty();
			startCar();
		},'xml');
	// show pop box info of slider images
	$("ul#carousels li a > img").live("mouseover", function(){
		clearTimeout(timr);
		$("span.pop").stop().animate({top:'154px'});
		var ref = $(this);
		timr = setTimeout(function(){ref.next().css("opacity",.8).stop().animate({top:'70px'});},10);
	}).live("mouseout", function(){
		clearTimeout(timr);
		var ref = $(this);
		timr = setTimeout(function(){ref.next().stop().animate({top:'154px'});},1);
	});
	$("#footer_bottom").after($("#below-footer").show());

	if($("#rcol").length){
		$("#content_area table:first, ").remove();
		$("#rcol").css("margin-right","0").parents("table:first").attr({cellspacing:"0",cellpadding:"0"}).parents("table:first").next().remove();
		$("#content_area").attr({cellspacing:"0",cellpadding:"0"});
		$("#rcol, .womenCollections").parents("td:first").css("padding","0");
		$("#content").prepend($("h1#special_design"));
	}
	$(".womenCollections, .menCollections, .category_landing_page").parents("td:first").css("padding","0").end().parents("table:first").attr({cellspacing:"0",cellpadding:"0"});
	$("#content table:first").css("float","left");
	if($(".womenCollections, .menCollections, .category_landing_page").length)
		$("td.colors_lines_light").parent().parent().find(">tr:gt(0):lt(3)").remove();
	$(".dynamic_scroller").cycle({
		prev:   '#prev',
		next:   '#next',
		pause:	1,
		timeout:5000,
		after:  onAfter,
		fx: 'scrollHorz'
	});
	$(".right_box #pause").toggle(function(){$('.dynamic_scroller').cycle('pause');$(this).attr("src","scrollercontrolls/paused.png");},function(){$('.dynamic_scroller').cycle('resume');$(this).attr("src","scrollercontrolls/pause.png");});
});
