// JavaScript Document

function initMainImage() {
	
	$("#rotate_buttons").html(
		"<a href='javascript:void(0);' class='main_img_btn_01'><img src='images/main_img_btn_gold.png' id='main_img_btn_gold_01' title='Click to pause' width='30' height='15' /></a>" +
		"<a href='javascript:void(0);' class='main_img_btn_01'><img src='images/main_img_btn_blue.png' id='main_img_btn_blue_01' title='Click to pause' width='30' height='15' /></a>" +
		"<a href='javascript:void(0);' class='main_img_btn_02'><img src='images/main_img_btn_gold.png' id='main_img_btn_gold_02' title='Click to pause' width='30' height='15' /></a>" +
		"<a href='javascript:void(0);' class='main_img_btn_02'><img src='images/main_img_btn_blue.png' id='main_img_btn_blue_02' title='Click to pause' width='30' height='15' /></a>" +
		"<a href='javascript:void(0);' class='main_img_btn_03'><img src='images/main_img_btn_gold.png' id='main_img_btn_gold_03' title='Click to pause' width='30' height='15' /></a>" +
		"<a href='javascript:void(0);' class='main_img_btn_03'><img src='images/main_img_btn_blue.png' id='main_img_btn_blue_03' title='Click to pause' width='30' height='15' /></a>" +
		"<a href='javascript:void(0);' class='main_img_btn_04'><img src='images/main_img_btn_gold.png' id='main_img_btn_gold_04' title='Click to pause' width='30' height='15' /></a>" +
		"<a href='javascript:void(0);' class='main_img_btn_04'><img src='images/main_img_btn_blue.png' id='main_img_btn_blue_04' title='Click to pause' width='30' height='15' /></a>"
	);

	$("#img_close").click(
        function() {
            document.getElementById('pop_video').style.visibility = 'hidden';
        }
    );
	
	if (isApple_iOS	()) {
		$('#main_image_01').html("<img src='images/main_01.png' alt='SeneGence International' width='980' height='430' />");
	}
	else {
		$('#main_image_01').html("<embed src='flash/main_01.swf' type='application/x-shockwave-flash' allowscriptaccess='always' width='980' height='430' wmode='transparent'></embed>");
	}
	
	var isLoop = true;

	rotateImages(isLoop);
	$('.main_img_buttons').attr('title', 'Click to pause');
	
	$('.main_img_btn_01').click(
		function() {
			showMain_01();
			isLoop = operateLoop(isLoop);			
		}
	);
	
	$('.main_img_btn_02').click(
		function() {
			showMain_02();
			isLoop = operateLoop(isLoop);			
		}
	);
	
	$('.main_img_btn_03').click(
		function() {
			showMain_03();
			isLoop = operateLoop(isLoop);			
		}
	);
	
	$('.main_img_btn_04').click(
		function() {
			showMain_04();
			isLoop = operateLoop(isLoop);			
		}
	);

	var country_id = getCountryID();

	$.ajax({
		type: "POST",
		url: "default.aspx/LoadMarketingBoxes",
		data: "{CountryID:" + country_id + "}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		error: function(XMLHttpRequest, textStatus, errorThrown){
			//alert(textStatus);
			//alert(arguments[0].responseText);
			return false;					
		},
		success: function(result) {
	
			BoxNo = getASPResultVars(result.d)["BoxNo"].split('+++');
			ImageTop = getASPResultVars(result.d)["ImageTop"].split('+++');;
			ImageBottom = getASPResultVars(result.d)["ImageBottom"].split('+++');;
			PageURL = getASPResultVars(result.d)["PageURL"].split('+++');;
			
			marketingBoxHTML = "";
			
			$(BoxNo).each(
				function(i) {
					if (BoxNo[i] != "") {
						n = i + 1;
						marketingBoxHTML = marketingBoxHTML + 
							"<div id='box_0" + n + "_section'>" +
								"<a href='" + PageURL[i] + "'><img id='box_0" + n + "_top' src='images/" + ImageTop[i] + "' alt='Box 0" + n + " Image' width='242' height='28' /></a><br />" +
								"<a href='" + PageURL[i] + "' id='box_0" + n + "_bottom'><img id='box_0" + n + "_full' src='images/space.png' alt='Box 0" + n + " Image' width='242' height='72' /></a>" +
							"</div>";
					}
				}
			);
			
			$("#box_shuttle").html(marketingBoxHTML);
	
			$("#box_01_section").hoverIntent(
				function () {
					$("#box_01_full").attr("src", "images/" + ImageBottom[0]);
					$("#box_01_bottom").slideDown("slow");
					$("#box_01_full").show();
				}, 
				function () {         
					$("#box_01_bottom").slideUp("slow");
				}
			);
			
			$("#box_02_section").hoverIntent(
				function () {
					$("#box_02_full").attr("src", "images/" + ImageBottom[1]);
					$("#box_02_bottom").slideDown("slow");
					$("#box_02_full").show();
				}, 
				function () {         
					$("#box_02_bottom").slideUp("slow");
				}
			);
			
			$("#box_03_section").hoverIntent(
				function () {
					$("#box_03_full").attr("src", "images/" + ImageBottom[2]);	
					$("#box_03_bottom").slideDown("slow");
					$("#box_03_full").show();
				}, 
				function () {         
					$("#box_03_bottom").slideUp("slow");
				}
			);
			
			$("#box_04_section").hoverIntent(
				function () {
					$("#box_04_full").attr("src", "images/" + ImageBottom[3]);	
					$("#box_04_bottom").slideDown("slow");
					$("#box_04_full").show();
				}, 
				function () {         
					$("#box_04_bottom").slideUp("slow");
				}
			);	
		}
    });
}

function rotateImages(isLoop) {
	
	var waitSec = 10 * 1000; // 10 seconds

	if( $('#main_image_01').is(':visible') ) {
		if (isApple_iOS	()) {
			waitSec = 5 * 1000;
		}
		else {
			waitSec = 10 * 1000;
		}
		$.doTimeout('show_02', waitSec, 
			function() {
				showMain_02();
				if (isLoop) {
					$.doTimeout('show_03', true);
					rotateImages(isLoop);					
				}
			}
		);
		
	}
	
	if( $('#main_image_02').is(':visible') ) {
		if (isApple_iOS	()) {
			waitSec = 5 * 1000;
		}
		else {
			waitSec = 15 * 1000;
		}
		$.doTimeout('show_03', waitSec, 
			function() {
				showMain_03();
				if (isLoop) {
					$.doTimeout('show_04', true);
					rotateImages(isLoop);
				}	
			}
		);
	}
	
	if( $('#main_image_03').is(':visible') ) {
		if (isApple_iOS	()) {
			waitSec = 5 * 1000;
		}
		else {		
			waitSec = 20 * 1000;
		}
		$.doTimeout('show_04', waitSec, 
			function() {
				showMain_04();
				if (isLoop) {
					$.doTimeout('show_01', true);
					rotateImages(isLoop);
				}	
			}
		);
	}
	
	if( $('#main_image_04').is(':visible') ) {
		if (isApple_iOS	()) {
			waitSec = 5 * 1000;
		}
		else {		
			waitSec = 45 * 1000;
		}
		$.doTimeout('show_01', waitSec, 
			function() {
				showMain_01();
				if (isLoop) {
					$.doTimeout('show_02', true);
					rotateImages(isLoop);
				}	
			}
		);
	}		
}

function operateLoop(isLoop) {

	if (isLoop) {
		// Cancel the loop 
		$.doTimeout('show_01');
		$.doTimeout('show_02');
		$.doTimeout('show_03');
		$.doTimeout('show_04');
		isLoop = false;
		$('#main_img_btn_gold_01').attr('title', 'Click to rotate');
		$('#main_img_btn_blue_01').attr('title', 'Click to rotate');
		$('#main_img_btn_gold_02').attr('title', 'Click to rotate');
		$('#main_img_btn_blue_02').attr('title', 'Click to rotate');
		$('#main_img_btn_gold_03').attr('title', 'Click to rotate');
		$('#main_img_btn_blue_03').attr('title', 'Click to rotate');
		$('#main_img_btn_gold_04').attr('title', 'Click to rotate');
		$('#main_img_btn_blue_04').attr('title', 'Click to rotate');
		
		if( $('#main_image_01').is(':visible') ) {
			$('#main_img_btn_gold_01').attr('src', 'images/main_img_btn_stop.png');
		}
		if( $('#main_image_02').is(':visible') ) {
			$('#main_img_btn_gold_02').attr('src', 'images/main_img_btn_stop.png');
		}
		if( $('#main_image_03').is(':visible') ) {
			$('#main_img_btn_gold_03').attr('src', 'images/main_img_btn_stop.png');
		}
		if( $('#main_image_04').is(':visible') ) {
			$('#main_img_btn_gold_04').attr('src', 'images/main_img_btn_stop.png');	
		}
	}
	else {
		// Resume the loop	
		isLoop = true;				
		$('#main_img_btn_gold_01').attr('title', 'Click to pause');
		$('#main_img_btn_blue_01').attr('title', 'Click to pause');
		$('#main_img_btn_gold_02').attr('title', 'Click to pause');
		$('#main_img_btn_blue_02').attr('title', 'Click to pause');
		$('#main_img_btn_gold_03').attr('title', 'Click to pause');
		$('#main_img_btn_blue_03').attr('title', 'Click to pause');
		$('#main_img_btn_gold_04').attr('title', 'Click to pause');
		$('#main_img_btn_blue_04').attr('title', 'Click to pause');	
		

		$('#main_img_btn_gold_01').attr('src', 'images/main_img_btn_gold.png');
		$('#main_img_btn_gold_02').attr('src', 'images/main_img_btn_gold.png');
		$('#main_img_btn_gold_03').attr('src', 'images/main_img_btn_gold.png');
		$('#main_img_btn_gold_04').attr('src', 'images/main_img_btn_gold.png');
		
		rotateImages(isLoop);
	}
	
	return isLoop;	
}

function showMain_01() {

	if (isApple_iOS	()) {
		$('#main_image_01').html("<img src='images/main_01.png' alt='SeneGence International' width='980' height='430' />");
	}
	else {	
		$('#main_image_01').html("<embed src='flash/main_01.swf' type='application/x-shockwave-flash' allowscriptaccess='always' width='980' height='430' wmode='transparent'></embed>");
	}
	
	$('#main_image_01').show();
	$('#main_image_02').hide();
	$('#main_image_03').hide();
	$('#main_image_04').hide();
	
	$('#main_img_btn_gold_01').show();
	$('#main_img_btn_blue_01').hide();
	$('#main_img_btn_gold_02').hide();
	$('#main_img_btn_blue_02').show();
	$('#main_img_btn_gold_03').hide();
	$('#main_img_btn_blue_03').show();
	$('#main_img_btn_gold_04').hide();
	$('#main_img_btn_blue_04').show();	
}

function showMain_02() {

	if (isApple_iOS	()) {
		$('#main_image_02').html("<img src='images/main_02.png' alt='SeneGence International' width='980' height='430' />");
	}
	else {		
		$('#main_image_02').html("<embed src='flash/main_02.swf' type='application/x-shockwave-flash' allowscriptaccess='always' width='980' height='430' wmode='transparent'></embed>");
	}

	$('#main_image_01').hide();
	$('#main_image_02').show();
	$('#main_image_03').hide();
	$('#main_image_04').hide();
	
	$('#main_img_btn_gold_01').hide();
	$('#main_img_btn_blue_01').show();
	$('#main_img_btn_gold_02').show();
	$('#main_img_btn_blue_02').hide();
	$('#main_img_btn_gold_03').hide();
	$('#main_img_btn_blue_03').show();
	$('#main_img_btn_gold_04').hide();
	$('#main_img_btn_blue_04').show();	
}

function showMain_03() {
	
	if (isApple_iOS	()) {
		$('#main_image_03').html("<img src='images/main_03.png' alt='SeneGence International' width='980' height='430' />");
	}
	else {		
		$('#main_image_03').html("<embed src='flash/main_03.swf' type='application/x-shockwave-flash' allowscriptaccess='always' width='980' height='430' wmode='transparent'></embed>");
	}
	
	$('#main_image_01').hide();
	$('#main_image_02').hide();
	$('#main_image_03').show();
	$('#main_image_04').hide();	
	
	$('#main_img_btn_gold_01').hide();
	$('#main_img_btn_blue_01').show();
	$('#main_img_btn_gold_02').hide();
	$('#main_img_btn_blue_02').show();
	$('#main_img_btn_gold_03').show();
	$('#main_img_btn_blue_03').hide();
	$('#main_img_btn_gold_04').hide();
	$('#main_img_btn_blue_04').show();			
}

function showMain_04() {
	
	if (isApple_iOS	()) {
		$('#main_image_04').html("<img src='images/main_04.png' alt='SeneGence International' width='980' height='430' />");
	}
	else {		
		$('#main_image_04').html("<embed src='flash/main_04.swf' type='application/x-shockwave-flash' allowscriptaccess='always' width='980' height='430' wmode='transparent'></embed>");
	}
	
	$('#main_image_01').hide();
	$('#main_image_02').hide();
	$('#main_image_03').hide();
	$('#main_image_04').show();	
	
	$('#main_img_btn_gold_01').hide();
	$('#main_img_btn_blue_01').show();
	$('#main_img_btn_gold_02').hide();
	$('#main_img_btn_blue_02').show();
	$('#main_img_btn_gold_03').hide();
	$('#main_img_btn_blue_03').show();
	$('#main_img_btn_gold_04').show();
	$('#main_img_btn_blue_04').hide();			
}

function isApple_iOS() {
	
	var isApple_iOS = false;
	
	if (navigator.userAgent.match(/iPhone/i)) {
		isApple_iOS = true;
	}
	if (navigator.userAgent.match(/iPod/i)) {
		isApple_iOS = true;
	}
	if (navigator.userAgent.match(/iPad/i)) {
		isApple_iOS = true;
	}
	
	return isApple_iOS;
}

$(document).ready(function() {initMainImage();});
