//default value for states dropdown is USA states
var USAStates = '<option selected="selected"> </option>';
for (var i in states[1]){
	USAStates=USAStates+'<option value="'+i+'">'+states[1][i]+'<\/option>';
}
$('#sel_mailingForm_state').html(USAStates);

// onload
$(document).ready(function(){
	countrySelectManipulation();
	stateSelectManipulation();
	$('#buydvd_options_fake span').filter(':first').css('display','none');
	// for inputs
	if ($('#inp_mailingForm_email').val().length>0) {$('label.inp_mailingForm_email').hide();} 
	$('label.inp_mailingForm_email').click(function(){$(this).hide();});
	$('#inp_mailingForm_email').click(function(){$('label.inp_mailingForm_email').hide();}).blur(function(){if($(this).val().length<1)$('label.inp_mailingForm_email').show()}).focus(function(){$('label.inp_mailingForm_email').hide()});
	if ($('#inp_mailingForm_city').val().length>0) {$('label.inp_mailingForm_city').hide();} 
	$('label.inp_mailingForm_city').click(function(){$(this).hide();});
	$('#inp_mailingForm_city').click(function(){$('label.inp_mailingForm_city').hide();}).blur(function(){if($(this).val().length<1)$('label.inp_mailingForm_city').show()}).focus(function(){$('label.inp_mailingForm_city').hide()});

	// Embed btn
	$('#content .embed').click(function(){
		$('#content .embed_text').toggle();
	});
	$('#content .embed_text input').click(function(){$(this).select();});
	
	// setup ul.tabs to work as tabs for each div directly under div.panes 
    $("ul.tabs").tabs("div.panes > div"); 
	$("ul.menu-tabs").tabs("div.menu-panes > div", {
		effect: 'fade'
	});
	
	// Share page
	var shareFormRecipient = $('#shareForm_recipient');
	var shareFormRecipientText = $('.textarea .shareForm_recipient_text');
	var shareFormRecipientLabel = $('.shareForm_recipient_label');
	(shareFormRecipient.val()=='')?shareFormRecipientText.show():shareFormRecipientText.hide();
	shareFormRecipientLabel.click(function(){shareFormRecipient.focus();shareFormRecipientText.hide();});
	shareFormRecipientText.click(function(){$(this).hide();shareFormRecipient.focus();});
	shareFormRecipient.click(function(){shareFormRecipientText.hide();}).blur(function(){if($(this).val()=='')shareFormRecipientText.show();}).focus(function(){shareFormRecipientText.hide();});
	
	/* Awards Showing */
	window.elm = $('.awards');
	window.timer1, window.timer2, window.elmValue, window.flag;
	window.interval = 5000;
	window.awardsShowNum = 3;
	window.awardsStart = 0;
	window.elmValue = window.randValue = new Array(awardsShowNum);
	
	elmFilling();
	elm.show();
	startFilling();
	
	$("a.light").lightbox({fileLoadingImage:'/design/images/loading.gif', fileBottomNavCloseImage : '/design/images/lightbox-btn-close.gif'}); 
});

function countrySelectManipulation(){
	// for selects with first <option>&nbsp;<\/option>
	$('#sel_mailingForm_country_fake span').filter(':first').css('display','none');
	//for selects
	$('label.sel_mailingForm_country').mousedown(function(){$('.sel_mailingForm_country div.center_a').mousedown();});
	$('.sel_mailingForm_country div.optionsDivInvisible > span').mousedown(function(){$('label.sel_mailingForm_country').hide();});
	$('.sel_mailingForm_country div.scroller-container > span').click(function(){$('label.sel_mailingForm_country').hide();});
	if ($('#sel_mailingForm_country').val().length>1) {$('label.sel_mailingForm_country').hide();}
	$('#vsel_mailingForm_country').focus(function(){if($('.selectArea.sel_mailingForm_country .optionsDivInvisible').css('display')=='none')$('label.sel_mailingForm_country').hide();}).blur(function(){if($(this).val()=='')$('label.sel_mailingForm_country').show()});
}

function stateSelectManipulation(){
	// for selects with first <option>&nbsp;<\/option>
	$('#sel_mailingForm_state_fake span').filter(':first').css('display','none');
	//for selects
	$('label.sel_mailingForm_state').mousedown(function(){$('.sel_mailingForm_state div.center_a').mousedown();});
	$('.sel_mailingForm_state div.optionsDivInvisible > span').mousedown(function(){$('label.sel_mailingForm_state').hide();});
	$('.sel_mailingForm_state div.scroller-container > span').click(function(){$('label.sel_mailingForm_state').hide();});
	if ($('#sel_mailingForm_state').val().length>1) {$('label.sel_mailingForm_state').hide();}
	$('#vsel_mailingForm_state').focus(function(){if($('.selectArea.sel_mailingForm_state .optionsDivInvisible').css('display')=='none')$('label.sel_mailingForm_state').hide();}).blur(function(){if($(this).val()=='')$('label.sel_mailingForm_state').show()});
}

function elmFilling(){
	elm.html('');
	if (awardsStart + 1 > awardsNum) {
		awardsStart = 0;
	}
	for (var i = 1; i <= awardsShowNum; i++) {
		if (awards[awardsStart]) {
			elm.html(elm.html() + '<img src="' + awards[awardsStart] + '" alt="" \/>');
		}
		awardsStart++;
	}
}
function startFilling(){
	timer1 = setInterval(showFilling,interval);
}
function stopFilling(){
	clearInterval(timer1);
}
function showFilling(){
	elm.fadeOut('slow');
	timer2 = setTimeout(showItNow, 1000);
}
function showItNow(){
	elmFilling();
	elm.fadeIn('slow');
	clearInterval(timer2);
}
function playTrailer(){
    $('#content .embed_text input').val(trailerEmbed);
	$('#content .trailer_btns .embed').show();
}
function statesChanger(){ // For States changing 
	var tempStates = '<select id="sel_mailingForm_state" class="sel_mailingForm_state" name="mailingForm_stateId"><option selected="selected"> </option>';
	if (states[$('#sel_mailingForm_country').val()]){
		for (var j in states[$('#sel_mailingForm_country').val()]){
			tempStates=tempStates+'<option value="'+j+'">'+states[$('#sel_mailingForm_country').val()][j]+'<\/option>';
		}
		tempStates = tempStates + '<\/select>';
	}
	$('div.mailingForm_state > *').unbind();
	$('div.sel_mailingForm_state').remove();
	$('label.sel_mailingForm_state').after(tempStates);
	changeSelects('div.mailingForm_state');
	stateSelectManipulation();
	$('label.sel_mailingForm_state').show();
}


jQuery(document).ready(function(){
    if (jQuery('#scroller').length) {
        jQuery('#scroller_in > a, #scroller').show();
        jQuery("#slidetabs").tabs("#scroller_images > div", {
            effect: 'fade',
            fadeOutSpeed: 1500,
            fadeInSpeed: 1500,
            rotate: true,
            tabs: 'span'
        }).slideshow({
            next: '.next',
            prev: '.prev',
            autoplay: true,
            interval: 5000,
            autopause: false,
            clickable: false
        });
    }
});
