/* + Vimeo + */
var videoUrl = 'http://vimeo.com/13273104';
var endpoint = 'http://www.vimeo.com/api/oembed.json';
var callback = 'embedVideo';
var url = endpoint + '?url=' + encodeURIComponent(videoUrl) + '&callback=' + callback + '&width=710&height=310&autoplay=true';
function embedVideo(video) {
	document.getElementById('embed').innerHTML = unescape(video.html);
}
function init() {
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', url);
	document.getElementsByTagName('head').item(0).appendChild(js);
}
/* - Vimeo - */

function submitCart(nr){	
	 $('#amountForm_'+nr).submit();
}
function reset(id){
	oldVal = $('#'+id).val();
	$('#'+id).val('');
	if(!oldVal){
		$('#'+id).removeClass('cross');
		$('#'+id).blur();
	}
}

function is_ie() {
	
	var browserName=navigator.appName; 

	if ( browserName == "Microsoft Internet Explorer") {
		return true;
	} else {
		return false;
	}

}


function check_payment(){
	var $elem = $('input[name="shipping"]:checked');
	
	if($elem.val() == 1){
		show_payment(true);
	}else{
		show_payment(false);
	}
}

function show_payment($show){
	var $elem = $('#paymentMethodsDiv');
	if($show){
		$elem.show();
		$elem.find('input').removeAttr('disabled');
	}else{
		$elem.hide();
		$elem.find('input').attr('disabled','');
	}
	
}

  
// JavaScript Document
$(document).ready(function () {
	
	$('input[name="shipping"]').click(function(e){
		check_payment();
	});
	check_payment();
	
	var mactest=navigator.userAgent.indexOf("Mac")!=-1

	if ( !mactest ){
		$('body').append('<link rel="stylesheet" type="text/css" href="/Fromanteel/assets/css/pc.style.css">');
	}

	if ( $('#shipstreet').length && $('#shipstreet').val() ){
		$('#shipping_block').show();
		$("input[id='shippingAddressBool1']").prop('checked', true);
	}else{
		$('#shipping_block').hide();
		$("input[id='shippingAddressBool0']").prop('checked', true);

	}
	
	$("input[name='shippingAddressBool']").change(function(){
		if ($("input[name='shippingAddressBool']:checked").val() == '1') {
			$('#shipping_block').show();
			if ($.browser.msie) {
				// IE fires multiple resize events while you are dragging the browser window which
				// causes it to crash if you try to update the scrollpane on every one. So we need
				// to throttle it to fire a maximum of once every 50 milliseconds...
				if (!throttleTimeout) {
					throttleTimeout = setTimeout(function(){
						api.reinitialise();
						throttleTimeout = null;
					},50);
				}
			} else {
				api.reinitialise();
			}
		}
		else{
			$('#shipping_block').hide();
			if ($.browser.msie) {
				// IE fires multiple resize events while you are dragging the browser window which
				// causes it to crash if you try to update the scrollpane on every one. So we need
				// to throttle it to fire a maximum of once every 50 milliseconds...
				if (!throttleTimeout) {
					throttleTimeout = setTimeout(function(){
						api.reinitialise();
						throttleTimeout = null;
					},50);
				}
			} else {
				api.reinitialise();
			}
		}
	});


	$('#circleBuynow').click(function() {	
	
		$('#buynow').submit();
	
	});


//alert($(".topNavigation").find('.highlight').attr('title') );

	/* + active nav + */
	if ( !is_ie() ){
		
		var activeNav = $(".topNavigation").find('.highlight');
		if ( activeNav.length ){
			activeNav.parent().append('<span class="activearrow"></span>');		
		}else{

			var companycheck = $('#activecompany').length ;
			
			if ( companycheck ){

				$('#company-16').append('<span class="activearrow"></span>');				
			}
		}
	}
	/* - active nav - */
	
	/* + toggle .box + */
	var paneHeight  = '';//define empty pane height
	$('.arrowup').click(function() {

		$('.arrowup').hide();

		if ( paneHeight == '' )	paneHeight = $('.jspPane').height();// set pane height if not defined			

		if ( $('.jspPane').height() == '0' ){
			 $('.jspPane').animate({
				height: paneHeight,
				'padding-top': '110px',
				'padding-bottom': '30px'
			  }, 1000, function() {
				  $('.arrowup').show();
				$('.jspPane').css('overflow', 'auto');
				$('.arrowup').css('bottom', '45px');
				$('.arrowup').css('background', 'url("/Fromanteel/assets/images/arrow-up.png")');
			  });		
		}else{
			 $('.jspPane').animate({
				height: '0',
				'padding-top': '110px'
			  }, 1000, function() {
				  $('.arrowup').show();
				 $('.jspPane').css('overflow', 'hidden');
				 $('.arrowup').css('bottom', '0');
				 $('.arrowup').css('background', 'url("/Fromanteel/assets/images/arrow-down.png")');
			  });
		}
	});
	
	$('.arrowdown').click(function() {

		if ( paneHeight == '' )	paneHeight = $('.contentframe.bottom .box').height();// set pane height if not defined			

		if ( $('.contentframe.bottom .box').height() == '0' ){
			 $('.contentframe.bottom .box').animate({
				height: paneHeight,
				'padding-bottom': '40px'
			  }, 1000, function() {
				$('.jspPane').css('overflow', 'auto');
				$('.arrowdown').css('bottom', '45px');
				$('.arrowdown').css('background', 'url("/Fromanteel/assets/images/arrow-down.png")');
			  });		
		}else{
			 $('.contentframe.bottom .box').animate({
				height: '0',
				'padding-bottom': '0'
			  }, 1000, function() {
				 $('.jspPane').css('overflow', 'hidden');
				 $('.arrowdown').css('bottom', '0');
				 $('.arrowdown').css('background', 'url("/Fromanteel/assets/images/arrow-up.png")');
			  });
		}
	});
	
	


	/* - toggle .box - */
	
	
	$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_square', social_tools: false,default_width: 700,default_height: 344 });
	
	// + slide url trigger + //
	$(".slideTrigger").click(function(event) {  //check if clicked
		var activeslide; var href
		if ( activeslide = $("#supersized").find('.activeslide a') ){ // find active slide
			if ( href = activeslide.attr("href") ){ //does href exist
				if ( href.match(/vimeo/) ){
					$.prettyPhoto.open(href);
				}else{
					window.location= href; // go to url
				}
			}
		}
		return false;
	});
	// + slide url trigger + //


	
	// + STORE LOCATOR + //
	
	$("#storeLocationForm").submit(function(){	
	
		var country = $("#country").val();		
		var city 	= $("#city").val();	
	
		var url = '/stores/';
		
		if ( country.length ){		
			url += country + '/';			
		}
		if ( city.length ){		
			url += city + '/';			
		}
		
		location.href = url;

		return false;
	
	});
	
	$("#country").change(function(){	
		var country = $(this).val();		
		$.ajax({
			url: "/stores/",
			global: false,
			type: "POST",
			cache: false,
			data: "ajax=true&country="+country, //fill post
			dataType: "html",
			success: function(data){
				$("#city option").each(function(){	
					$(this).remove(); //remove options
				});
				$('#city').append(data); //fill dropdown
			}
		});
	});
	
	
	// - STORE LOCATOR - //
	
	
	$(".closeBtn").click(function(event) {  
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   
		$(this).parent().remove();
	
	});
	
	
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? this.t : "";

		var image = ( this.href.indexOf("#") < 0 ) ? "<img src='"+ this.href +"' alt='Image preview' /><br/>" : "";
		xOffset = 10;
		yOffset = 30;

		
		$("body").append("<div id=\"preview\" />");								 
		$("#preview")
			.html(image + c)
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
	
	/* + Vimeo + */
	//$( '#embed' ).css('display','none');
	$( '#videocontainer' ).click( function() {
		$.prettyPhoto.open('http://vimeo.com/13273104');
		//$( '#videocontainer' ).css('display','none');
		//$( '#embed' ).css('display','block');
		//init();	
		return false;
	});
	/* - Vimeo - */
	
	//*

	
	if ( $("#scroll-pane").length ){
		
		$("#scroll-pane").jScrollPane({ showArrows: $(this).is('.arrow') });
		
		/* + masonry + */
		/* 
		var is_masonry = ( $("#fw").length ) ? true: false;
		
		var api = $("#scroll-pane").data('jsp');
		var throttleTimeout;
		
		$(window).bind('resize',function(){
			if ( is_masonry ){
				$('#fw').masonry('destroy');
			}
			if ($.browser.msie) {
				// IE fires multiple resize events while you are dragging the browser window which
				// causes it to crash if you try to update the scrollpane on every one. So we need
				// to throttle it to fire a maximum of once every 50 milliseconds...
				if (!throttleTimeout) {
					throttleTimeout = setTimeout(function(){
						api.reinitialise();
						throttleTimeout = null;
					},50);
				}
			} else {
				api.reinitialise();
			}
			if ( is_masonry ){
				$('#fw').masonry( {itemSelector : '.fwBox'} );
			}
		});
		if ( is_masonry ){
			$('#fw').masonry( {itemSelector : '.fwBox'} );
		}
		*/
		
		/* - masonry - */
		
		
		
		var api = $("#scroll-pane").data('jsp');
		var throttleTimeout;
		
		$(window).bind('resize',function(){
			
			if ($.browser.msie) {
				// IE fires multiple resize events while you are dragging the browser window which
				// causes it to crash if you try to update the scrollpane on every one. So we need
				// to throttle it to fire a maximum of once every 50 milliseconds...
				if (!throttleTimeout) {
					throttleTimeout = setTimeout(function(){
						api.reinitialise();
						throttleTimeout = null;
					},50);
				}
			} else {
				api.reinitialise();
			}
			
		});
		
		
		
		
	}
	
	$(".toggle_container").hide();


	$("p.trigger").click(function(e){
		e.preventDefault();
		$(this).toggleClass("active").parent().parent().next().find('.cartItem').slideToggle("slow");
		return false;
	});
	
	// + history effects + //
	$('.historyBack').click(function(event) {
		event.preventDefault();
		window.history.go(-1);
	});
	$('.historyNext').click(function(event) {
		event.preventDefault();
		history.go(+1);
	});
	$(".Jradio input").click(function(){					
		if($(this).is(':checked')) {
			$(this).parent().siblings('.active').removeClass('active');
			$(this).parent().addClass('active');
		}
	});
	// - history effects - //
	
	$("#bdate1, #bdate2, #bdate3").focus(function(){
		if($(this).val() == 'dd' || $(this).val() == 'mm' || $(this).val() == 'yyyy'){
			$(this).val('');
		}
	});
	
	// FORM INPUT: form effects
	$(":text").not('.ignore').each(function () {
		
		if ( $(this).attr("title") && !$(this).val() ) {
			$(this).val($(this).attr("title"));
		}else{
			if($(this).val()){
				$(this).removeClass('inputBlur');
				$(this).addClass('inputFocus');
				if($(this).hasClass('addCross')){
					$('<div class="inputDelete" style="float:right;position:relative;width:0;height:0;overflow:visible;cursor:pointer;" onclick="reset(\''+$(this).attr('id')+'\');"><div style="cursor:pointer;position:absolute;z-index:1;width:16px;height:18px;left:-16px;" /></div>').insertAfter(this);
					$(this).addClass('cross');
				}
			}
		}
		$(this).focus(function () {
			$(this).removeClass('inputBlur');
			$(this).addClass('inputFocus');
			if ($(this).val() === $(this).attr("title")) {
				$(this).val("");
			}
			if($(this).hasClass('addCross')){
				$('<div class="inputDelete" style="float:right;position:relative;width:0;height:0;overflow:visible;cursor:pointer;" onclick="reset(\''+$(this).attr('id')+'\');"><div style="cursor:pointer;position:absolute;z-index:1;width:16px;height:18px;left:-16px;" /></div>').insertAfter(this);
				$(this).addClass('cross');
			}
		}).blur(function () {
			if ($(this).val() === "") {
				$(this).addClass('inputBlur');
				$(this).removeClass('inputFocus');
				$(this).removeClass('cross');
				if($(this).hasClass('addCross'))
					$(this).next().remove();
				$(this).val($(this).attr("title"));
			}
		});
	
	});
	
});

