$(document).ready(function(){
	
	jsButtons.init();
	    
});



var jsButtons = {
	init: function () {
		
		$('ul#js-buttons').addClass('clearfix').addClass('subnav');
		$('ul#js-buttons li a').each(function(i){
			
			$(this).addClass('button');
			
			if (i == 0) {				
				$(this).attr('id', 'our-company')
			}
			
			if (i == 1) {
				$(this).attr('id', 'your-questions-answered');
			}

			if (i == 2) {
				$(this).attr('id', 'join-our-team');
			}		
			
			if (i == 3) {
				$(this).attr('id', 'our-teams');
			}	
			
		});
		
	}
}





function hideForJS() {
	$('html').addClass("js-enabled");
}

function collapsible() {
	$("div.collapsible").hide();
	$("div.expand").show();

	$("a[@rel='collapsible']").click(function(){
		$(this).parent('h4').toggleClass('active');
		if( jQuery.browser.msie && (jQuery.browser.version <= 7)) {
			$("div#" + $(this).attr('class') ).toggle();
		} else {
			$("div#" + $(this).attr('class') ).slideToggle('fast');
		}
	});
	
}


function clearInput() {
	$('input.clear-me').focus(function(){
		if($(this).attr('value') == '') {
			$(this).attr('value',$(this).attr('alt'));
		}
		else if( $(this).attr('value') == $(this).attr('alt') ) {
			$(this).attr('value','');
		}
	});
	$('input.clear-me').blur(function(){
		if($(this).attr('value') == '') {
			$(this).attr('value',$(this).attr('alt'));
		}
	});	
}


function autosubmit() {
	$('form.autosubmit :submit').hide();
	$('form.autosubmit > select').addClass('long-select').change(function(){
		if($(this).attr('disabled') == true) {
			$(this).attr('disabled',false);
		}
		else {
			$('form.autosubmit').submit();
		}
	});
}

var film_id = "";

function flashplayer() {
	$('#flashplayer').hide().append('<a href="#close" class="trailer-close">CLOSE</a>');

	$('.trailer-link').click(function(){
		dcsMultiTrack('DCS.dcsuri','/trailers/watchtrailer','WT.ti','Watch Trailer','WT.cg_s','Trailers','DCSext.trailer','1','WT.pn_sku', film_id);
		$('#flashplayer').show('slow');
		$('.trailer-link').hide();
		expand(true);
	});

	$('.trailer-close').click(function(){
		$('#flashplayer').hide();
		$('.trailer-link').show();
		expand(false);
	});	
}

function adder() {
	$('#add-me').click(function(){

		//get the values of the selected items
		//loop over the values in the selected box
		//only move the item over if it's not already there (this is a work around because 'disabled' doesn't work in IE)

		if(($('#cinemas-selected > OPTION').length +  $('#cinemas > OPTION:selected').length) < 6) {

			for(var i=0; i< $('#cinemas > OPTION:selected').length; i++){

				var value = $('#cinemas > OPTION:selected')[i].value;
				var option = $('#cinemas > OPTION:selected')[i];
				var found = false;

				for(var j=0; j<$('#cinemas-selected > OPTION').length; j++){
					if(value == $('#cinemas-selected > OPTION')[j].value) found = true;
				}

				if(!found){
					//then move it over to the selected box
					var oOption = document.createElement('OPTION');
					oOption.value = option.value;
					oOption.innerHTML = option.innerHTML;
					$('#cinemas-selected').append(oOption);

					option.disabled = 'disabled';
				}
			}

			$('#cinemas > OPTION:selected').attr('selected','');

			//$('#cinemas-selected').append( $('#cinemas > OPTION:selected').attr('selected','').attr('disabled','disabled').clone().attr('disabled','') );
		}
	});
	$('#remove-me').click(function(){
		$('#cinemas-selected > OPTION:selected').each(function (i) {
			$("#cinemas > OPTION[value=" + this.value + "]").attr('disabled','');			
		});				
		$('#cinemas-selected > OPTION:selected').remove();
	});
	$('#move-up').click(function(){
		if ($('#cinemas-selected > OPTION:selected:first').val() != $('#cinemas-selected > OPTION:eq(0)').val()) {
			$('#cinemas-selected > OPTION:selected').each(function (i) {
				swapOptions(this.index, this.index - 1);	
			});
		}
	});
	$('#move-down').click(function(){
		if($('#cinemas-selected > OPTION:selected:last').val() != $('#cinemas-selected > OPTION:eq(' + ($('#cinemas-selected OPTION').length - 1) + ')').val()) {
			var indexes = new Array();
			$('#cinemas-selected > OPTION:selected').each(function (i) {
				indexes.push(this.index);							
			});			
			indexes.reverse();
			for (var i = 0; i < indexes.length; i++) {
				var index = indexes[i];
				swapOptions(index, index + 1);				
			}			
 		}
	});
}

function swapOptions(selectedIndex, targetIndex) {
	var option1 = $('#cinemas-selected > OPTION')[selectedIndex];
	var value1 = option1.value;
	var text1 = option1.text;
	
	var option2 = $('#cinemas-selected > OPTION')[targetIndex];
	
	option1.value = option2.value;
	option1.text = option2.text;
	option1.selected = false;
	
	option2.value = value1;
	option2.text = text1;
	option2.selected = true;
}

function addStar() {
	$('.form').prev('h3').attr('display','none').before('<div></div><div class="form-sprite"></div><div></div>');
}

// IE6 expandleft fix
function expandLeft() {
	$('.expandleft').each(function(){
		if( $(this).height() <= $(this).parent('.i3').height() ) {
			$(this).css( 'height', $(this).parent('.i3').height() );
		}
		else {
			$(this).prev('.form').css( 'height', $(this).height() );
		}

	});
	
}

function toggleTncs() {
	$('#show-tncs').click(function(){
		$('#tncs').toggle();
	});
}

function faq() {
	$('.faq OL > LI > A').click(function(){	
		$(this).next('div').slideToggle('slow');
		return false;
	})
}

function addPrint() {
	$('#button-print').show().click(function(){
		window.print();
	});
}

function synopsis() {	
	$('.show-more').click(function(){
		$(this).parents('div.toggle-synopsis').children('div.summary').toggle();
		$(this).parents('div.toggle-synopsis').children('div.synopsis').toggle();
	});
	
}

function interceptIrelandSwitch() {
	$('select#cinema').change(function(){
		if($(this).attr('value') == '75' && $(this).children().length > 4) {
			$(this).attr('disabled','disabled');
			ireland();
		}
	});
}

function ireland() {
	$('#territory-switch').click();
}

/* alternate flash player functionality */

		function noFilms(){
			alert("Sorry, The player is out films at the moment!");
		}
		function resizeDIV(height) {
			document.getElementById('flashplayer').style.height = height + 'px';
			//document.getElementById('flashcontent').style.height = height + 'px';
			document.getElementById('alternate').style.height = height + 'px';
		}
		
function pwdInput() {
	$('#login-user').blur(function(){
		if($('#login-password').attr('value').length > 0) {
			$('#login-password').removeClass('form-password');
		}
	});
	$('#login-password').focus(function(){
		if($(this).attr('value').length == '') {
			$(this).removeClass('form-password');
		}
	});	
	$('#login-password').blur(function(){
		if($(this).attr('value').length == '') {
			$(this).addClass('form-password');
		}
	});
	$('#login-user').focus().blur();
}


// js bootstrap

function init() {
	$('input.clear-me').focus().blur();
	Shadowbox.init();
	pwdInput()
	collapsible();
	interceptIrelandSwitch();
	autosubmit();
	clearInput();
	//flashplayer();
	addStar();
	adder();
	//expandLeft();
	faq();
	addPrint();
	synopsis();
	toggleTncs();

}

hideForJS();
$().ready(function(){
	init();
});


