/* Copyright (c) 2009 Jurriaan Topper (iksi.tv) */

(function($) {
	function pageload(hash) {

	  if (!hash || hash == "/" || hash == "/index.php") {
	    hash = "/home";
	  }

	  var vars = hash.split("/");
	  var _href = vars[1];

	  if (vars[2]) {
	   // second variable
	   var _check = vars[2].substr(0,4);
	   if (_check == "page") {
	    // page
	    var _page = vars[2].substr(4,9);		
	    var _url = "files/ajax/"+_href+".php?page="+_page;
	   } else {
		 // id
		 var _url = "files/ajax/"+_href+".php?id="+vars[2];		
	   }

	   if (vars[3]) {
	    //_url = _url+"&item=100";
	    var _check = vars[3].substr(0,4);
	    if (_check == "page") {
	     // page
	     var _page = vars[3].substr(4,9);		
	     var _url = _url+"&page="+_page;
	    } else {
	      var _url = _url+"&id="+vars[3];
	    }
	   }

	  } else {
		
	   // just the page
	   var _url = "files/ajax/"+_href+".php";	
	  }	
	  _url = "/"+_url;

	   // set loading
	   $("#content > .content").css("display","none");
	   $("#content > .content").html("<div class=\"loading block\"><h1>loading...</h1></div>");
	   $('#content > .content').attr("id","content_loading");
	
	   $("#content > .content").css("display","block");
	
	   $.ajax({
	    url: _url,
	    cache: false,
	    success: function(html){
		 // set content
	   	 $("#content > .content").css("display","none");	
	     $("#content > .content").html(html);
	   	 $('#content > .content').attr("id","content_"+_href);  
		 //
		 if (_href == "contact") {
          setContactForm();
		 }
		
		 if (_href == "buy") {
		  setSoldOutForm();
		 }
		
		if (_href == "pants") {
			$(".group").fancybox();
			setSliders();
		}
		
	   	 //$("#content > .content").fadeIn(300);		
	   	 $("#content > .content").css("display","block");		
        

        // for new ajax links:
        $("a[class='ajax']").unbind("click");
		$("a[class='ajax']").click(function(){
			var hash = this.rel;
			hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);
			return false;
		});

	    }
	   });

	   var parts = document.title.split(" / ");
	   var siteTitle = parts[0];
	   // set title
	   var pageTitle = _href.substr(0,1).toUpperCase()+_href.substr(1);
	   var documentTitle = document.title.toString().split(" / ");
	   if (pageTitle != "Home") {
	    document.title = documentTitle[0]+" / "+pageTitle;	
	   } else {
        document.title = documentTitle[0];
	   }

	   // set body id
	   $('#wrapper').parent().attr("id",_href);
	
	 }

 $(document).ready(function() {
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "jquery_history.html");
	// set onlick event for buttons
	$("a[class='ajax']").click(function(){
		// 
		var hash = this.rel;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});

  // default beginpage or google link
  var _hash = window.location.toString().split(".com");
  _hash = _hash[1].replace(/^.*#/, '');
  pageload(_hash);

  //$(window).bind("resize", setPosition);

 });

})(jQuery);

function setContactForm() {
	$('#contactForm').ajaxForm( {
		target: '#contact_feedback',
		success: function() { 
			$('#contactForm').resetForm();                           
		}
    });
}

function setSoldOutForm() {
 $('#soldOutForm').ajaxForm( {
  target: '#soldout_feedback',
  success: function() { 
   $('#soldOutForm').resetForm();                           
  }
 });
}


// clear / restore
function clearDefault(el) {
 if (el.defaultValue==el.value) el.value = "";
}

function putDefault(el) {
 if (el.value=="") el.value = el.defaultValue;
}

function checkApp() {
 if (navigator.appName == 'Microsoft Internet Explorer') {
  return true;
 }
 return false;
}


//

function setSliders() {

			$("#slider-male").slider({
				value: 180,
				min: 169,
				max: 191,
				slide: function(event, ui) {
					if (ui.value == 169) {
						var value = 170;
						var pre = "< ";
					} else if (ui.value == 191) {
						var value = 190;
						var pre = "> ";
					} else {
						var value = ui.value;	
						var pre = "";
					}
					$("#male-label").html("I am male and "+pre+value+' cm');
					$("#male").val(ui.value);
					$("#gender").val("m");
					setAdvice();										
				}
			});
			
			$("#slider-female").slider({
				value: 170,				
				min: 164,
				max: 176,
					slide: function(event, ui) {
						if (ui.value == 164) {
							var value = 165;
							var pre = "< ";
						} else if (ui.value == 176) {
							var value = 175;
							var pre = "> ";
						} else {
							var value = ui.value;	
							var pre = "";
						}
						$("#female-label").html("I am female and "+pre+value+' cm');
						$("#female").val(ui.value);	
						$("#gender").val("f");						
						setAdvice();				
					}
			});	
			
			$("#slider-type").slider({
				value: 1,				
				min: 0,
				max: 2,
				slide: function(event, ui) {
					if (ui.value == 0) {
						var type = "skinny";
					} else if (ui.value == 1) {
						var type = "average";
					} else {
						var type = "heavy";
					}
					$("#type-label").html("I am "+type);
					$("#type").val(ui.value);
					setAdvice();					
				}
			});	
			
			$("#slider-like").slider({
				value: 1,					
				min: 0,			
				max: 2,
				slide: function(event, ui) {
					if (ui.value == 0) {
						var type = "tight";
					} else if (ui.value == 1) {
						var type = "regular";
					} else {
						var type = "baggy";
					}
					$("#like-label").html("I like my clothes "+type);
					$("#like").val(ui.value);
					setAdvice();						
				}
			});				
			//$("#amount").val($("#slider").slider("value"));
		
}

function setAdvice() {
	//alert($("#gender").val());
	// male advice
	if ($("#gender").val() == "m") {
		if ($("#male").val() == 169) {
			if ($("#type").val() == 0 || $("#type").val() == 0 && $("#like").val() == 0 || $("#type").val() == 1) {
				var advice = "S (male)";				
			}			
			if ($("#type").val() == 2 || $("#like").val() == 2) {
				var advice = "M (male)";
			}			
		}		
		if ($("#male").val() >= 170 && $("#male").val() <= 180) {
			if ($("#type").val() == 0 || $("#type").val() == 0 && $("#like").val() == 0 || $("#type").val() == 1) {
				var advice = "M (male)";
			}			
			if ($("#type").val() == 2 || $("#like").val() == 2) {
				var advice = "L (male)";
			}
		}	
		if ($("#male").val() > 180 && $("#male").val() <= 190) {
			if ($("#type").val() == 0 || $("#type").val() == 0 && $("#like").val() == 0 || $("#type").val() == 1) {
				var advice = "L (male)";
			}
			if ($("#type").val() == 2 || $("#like").val() == 2) {
				var advice = "XL (male)";				
			}
		}
		if ($("#male").val() == 191) {
			var advice = "XL (male)";
		}
	}
	
	if ($("#gender").val() == "f") {
		if ($("#female").val() == 164) {
			if ($("#type").val() == 0 || $("#type").val() == 0 && $("#like").val() == 0 || $("#type").val() == 1) {
				var advice = "XS (female)";				
			}			
			if ($("#type").val() == 2 || $("#like").val() == 2) {
				var advice = "S (female)";
			}			
		}		
		if ($("#female").val() >= 165 && $("#female").val() <= 170) {
			if ($("#type").val() == 0 || $("#type").val() == 0 && $("#like").val() == 0 || $("#type").val() == 1) {
				var advice = "S (female)";
			}			
			if ($("#type").val() == 2 || $("#like").val() == 2) {
				var advice = "M (female)";
			}
		}	
		if ($("#female").val() > 170 && $("#female").val() <= 175) {
			if ($("#type").val() == 0 || $("#type").val() == 0 && $("#like").val() == 0 || $("#type").val() == 1) {
				var advice = "M (female)";
			}
			if ($("#type").val() == 2 || $("#like").val() == 2) {
				var advice = "L (female)";				
			}
		}
		if ($("#female").val() == 176) {
			var advice = "L (female)";
		}
	}	
	
	$("#suggest").html(advice);
}