
jQuery(document).ready(function() {
	
	//nav_main space li's for language change
	adjustNav();
	setInterval( "adjustNav()", 1000 );
	
	//watermark
//	$("#ucHeader_lb_user").Watermark("email...");
//	$("#ucHeader_lb_pass").Watermark("password...");
//	$("#ucHeader_lb_email").Watermark("Enter your email...");	
//	$("#search_bar_input").Watermark("Enter item or order number...");		
	
//	jQuery(function($){
//		$("#email_form_text input").Watermark("Enter your email...");
//	});	
	
	//question and answers slider
	$('.slide_title').click(function() {
		var isOpen = $(this).parent().find('.slide_content').hasClass('block');
		$('.slide_content').slideUp().removeClass('block').addClass('hidden').parent().find('.toggle_switch').css('background-position','0 0')	
			if (!isOpen) {
				$(this).parent().find('.slide_content').slideDown().removeClass('hidden').addClass('block');
				$(this).parent().find('.toggle_switch').css('background-position','10px 0');
			} 
	});
	
	//register validation & mask for phone number
//	$('#register_form').validate({
//		rules: {
//			first_name: {
//				required: true,
//				minlength: 2
//			},
//			last_name: {
//				required: true,
//				minlength: 2
//			},
//			email: {
//				required: true,	
//				email: true
//			},
//			phone_1: {
//				required: true,
//				digits: true,
//				minlength: 3
//			},
//			phone_2: {
//				required: true,	
//				digits: true,
//				minlength: 3
//			},
//			phone_3: {
//				required: true,	
//				digits: true,
//				minlength: 4
//			},
//			agree: "required"
//		},
//		messages: {
//			first_name: "Please enter your first name.",
//			last_name: "Please enter your last name.",
//			email: {
//				required: "Please enter your email addresss.",
//				email: "Please enter a valid email addresss."
//			},
//			phone_1: "Please enter a valid phone number",
//			phone_2: "Please enter a valid phone number",
//			phone_3: "Please enter a valid phone number",
//			agree: "You must agree to the Reship.com terms & conditions to continue."
//		},								 
//		wrapper: "div",
//		invalidHandler: function(e, validator) {
//			var errors = validator.numberOfInvalids();
//			if (errors) {
//				var message = errors == 1 ? 'There is an error with a field. It has been highlighted below' : 'There was an error with ' + errors + ' fields. They have been highlighted below';
//				$("#summary").html(message);
//				$("#summary").show();
//			} else {
//				$("#summary").hide();
//			}
//		}, groups: {
//			phone: "phone_1 phone_2 phone_3",
//			zip: "zip_1 zip_2"
//        }, errorPlacement: function(error, element) { 
//			if (element.attr("name") == "phone_1" || element.attr("name") == "phone_2" || element.attr("name") == "phone_3")
//			{error.insertAfter("#phone_3");}
//			else if (element.attr("name") == "zip_1" || element.attr("name") == "zip_2")
//			{error.insertAfter("#zip_2");}
//			else if (element.attr("name") == "agree")
//			{error.insertAfter("#agree_msg");}
//			else
//				error.insertAfter(element);
//		}
//	})
//	
	//get phone & zip on registration form and submit
	$('#reg_form_btn').click(function() {
		$('#phone').val("(" + $('#phone_1').val() + ") " + $('#phone_2').val() + " " +$('#phone_3').val());
		$('#register_form').submit();
	})
		
	$('#reg_form_btn_123').click(function() {
		$('#phone').val("(" + $('#phone_1').val() + ") " + $('#phone_2').val() + " " +$('#phone_3').val());
		$('#register_form').submit();
    })

    $('div#new_header .inner_btm a.banner').lightBox();

    /* Tutorial Module */

    //overlay and centering controls
    $('.top_banner').click(function () {
        $('#tutorial_module').fadeIn();
        //auto play on load
        set_tut_html(1);
        $('#tutorial_module .inner_box .right_col .tut_container').append('<iframe width="640" height="480" src="http://www.youtube.com/embed/WH0eaGOh_fs" frameborder="0" allowfullscreen></iframe>');
    });

    $('#tutorial_module .overlay, #tutorial_module .inner_box .close_btn').click(function () {
        $('#tutorial_module').fadeOut(500);
        //remove video from running in bg
        $('#tutorial_module .inner_box .right_col .tut_container').html('');
    });

    $(window).resize(function () {
        setTutTop();
    });

    function setTutTop() {
        pageHeight = $(window).height();
        pageWidth = $(window).width();
        $('#tutorial_module .inner_box').css({ 'top': (pageHeight - $('#tutorial_module .inner_box').height()) / 2, 'left': (pageWidth - $('#tutorial_module .inner_box').width()) / 2 });
    }
    setTutTop();

    //set video to nav
    var tut_html;
    function set_tut_html(tut_numer) {
        tut_html = '<video controls="controls"  autoplay="autoplay" poster="/tutorial_videos/tut_vid_' + tut_numer + '.jpg" width="640" height="360"  onclick="if(/Android/.test(navigator.userAgent))this.play();">'
						+ '<source src="/tutorial_videos/tut_vid_' + tut_numer + '.mp4" type="video/mp4" />'
						+ '<source src="/tutorial_videos/tut_vid_' + tut_numer + '.webm" type="video/webm" />'
						+ '<source src="/tutorial_videos/tut_vid_' + tut_numer + '.ogv" type="video/ogg" />'
						+ '<object type="application/x-shockwave-flash" data="/tutorial_videos/flashfox.swf" width="640" height="360" style="position:relative;">'
						+ '<param name="movie" value="/tutorial_videos/flashfox.swf" />'
						+ '<param name="allowFullScreen" value="true" />'
						+ '<param name="flashVars" value="autoplay=true&amp;controls=true&amp;loop=true&amp;src=tut_vid_' + tut_numer + '.mp4" />'
						+ '<embed src="/tutorial_videos/flashfox.swf" width="640" height="360" style="position:relative;"  flashVars="autoplay=true&amp;controls=true&amp;loop=true&amp;poster=/tutorial_videos/tut_vid_' + tut_numer + '.jpg&amp;src=tut_vid_' + tut_numer + '.mp4"	allowFullScreen="true" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_en" />'
						+ '<img alt="tutorial_video" src="/tutorial_videos/tut_vid_' + tut_numer + '.jpg" style="position:absolute;left:0;" width="640" height="360" title="Video playback is not supported by your browser" />'
						+ '</object>'
						+ '</video>';
    }

    $('#tutorial_module .inner_box .left_col a').click(function () {
        $('#tutorial_module .inner_box .left_col a').each(function (index) {
            $(this).removeClass('active')
        });
        $(this).addClass('active')
        $('#tutorial_module .inner_box .right_col .tut_container').html('');
        if ($(this).hasClass("item_1")) {
            //set_tut_html(1);
            $('#tutorial_module .inner_box .right_col .tut_container').append('<iframe width="640" height="480" src="http://www.youtube.com/embed/WH0eaGOh_fs" frameborder="0" allowfullscreen></iframe>');
        } else if ($(this).hasClass("item_2")) {
            //set_tut_html(2);
            $('#tutorial_module .inner_box .right_col .tut_container').append('<iframe width="640" height="480" src="http://www.youtube.com/embed/D6luEAP4Ns4" frameborder="0" allowfullscreen></iframe>');
        } else if ($(this).hasClass("item_3")) {
            //set_tut_html(3);
            $('#tutorial_module .inner_box .right_col .tut_container').append('<iframe width="640" height="480" src="http://www.youtube.com/embed/SZI-ZAtYG28" frameborder="0" allowfullscreen></iframe>');
        } 
    })


}); 

//same as billing checked
$('#same_as_billing').click(function() {
	var add_arr = new Array('country','street','city','province','zip')
	if ($('#same_as_billing').is(':checked')) {
		for (val in add_arr)
		{
			$('#'+add_arr[val]+'_ship').val($('#'+add_arr[val]+'_bill').val());
			$('#'+add_arr[val]+'_ship').css({'background':'#ccc','color':'#666'});
			$('#'+add_arr[val]+'_ship').attr('readonly', 'readonly');
		}
	} else {
		for (val in add_arr)
		{
			$('#'+add_arr[val]+'_ship').val(" ");
			$('#'+add_arr[val]+'_ship').css({'background':'#fff','color':'#000'});
			$('#'+add_arr[val]+'_ship').removeAttr('readonly');
		}		
	}
});

$('#country_bill, #street_bill, #city_bill, #province_bill, #zip_bill').change(function() {
	var add_arr = new Array('country','street','city','province','zip')
	if ($('#same_as_billing').is(':checked')) {
		for (val in add_arr)
		{
			$('#'+add_arr[val]+'_ship').val($('#'+add_arr[val]+'_bill').val());
		}
	}								   
});

//cvv2 tool tip
$(".cvv2_help").hover(function(e) {						 
	$(this).mousemove(function(e) {
		var tipY = e.pageY - ($('#cvv2Div').height()/2);
		var tipX = e.pageX + 20;
		$("#cvv2Div").css({'top': tipY, 'left': tipX});
	});
	$("#cvv2Div").fadeIn("fast");
}, function() {
	$("#cvv2Div").stop(true,true).fadeOut("fast");
});


//consolidate tool tip
$(".consolidate_help").hover(function(e) {						 
	$(this).mousemove(function(e) {
		var tipY = e.pageY - ($('#consolidateDIV').height()/2);
		var tipX = e.pageX + 20;
		$("#consolidateDIV").css({'top': tipY, 'right': tipX});
	});
	$("#consolidateDIV").fadeIn("fast");
}, function() {
	$("#consolidateDIV").stop(true,true).fadeOut("fast");
});


//adjust nav item widths based on selected language
function adjustNav() {	
	var total_btns_width = 0;
	var total_btns = 0;
	var gap = 0;
	$('#nav_main li').each(function(index) {
		total_btns_width += $(this).width();
		total_btns++;	
	});
	gap = ($('#nav_main').width() - total_btns_width)/(total_btns+1);
	$('#nav_main li').each(function(index) {			
		$(this).css('margin-left',gap);	
	});
	$('#sign_in_btns').css('padding-left',gap);
	
	//turn btns on
	$('#top_bar').css('visibility','visible');
	$('#nav_main').css('visibility','visible');
}

/* Google translate */
function googleTranslateElementInit() {
	new google.translate.TranslateElement({
		pageLanguage: 'en',
		layout: google.translate.TranslateElement.InlineLayout.SIMPLE
	}, 'google_translate_element');
}
/* /Google translate */
