/**
 * @author X-PORT
 */

/*------------------------------------ Page Change ------------------------------------*/
$(document).ready(function() {
	$("select[id=pages]").change(function() {
		window.location = $('select[id=pages] option:selected').val();
	});
});

/*------------------------------------ Media Change ------------------------------------*/
$(document).ready(function() {
	$("select[id=category]").change(function() {
		window.location = $('select[id=category] option:selected').val();
	});
});

/*------------------------------------ Tool Tips ------------------------------------*/
$(function() {
	$('.pretty').tooltip( {
		track : true,
		delay : 0,
		showURL : false,
		showBody : " - ",
		extraClass : "pretty",
		fixPNG : true,
		opacity : 0.95,
		left : -120
	});
});

/*------------------------------------ Play Media ------------------------------------*/
$(document).ready(function() {
	$(".media").click(function() {
		var url = $(this).attr("href");
		$('#bgsound').attr("src", url);
		$('#bgsound').attr("src", url);
		return false;
	});
});

/*------------------------------------ CSS Vertical Menu ------------------------------------*/
var menuids = new Array("verticalmenu")
// Enter id(s) of UL menus, separated by commas
var submenuoffset = -2
// Offset of submenus from main menu. Default is -2 pixels.

function createcssmenu() {
	for ( var i = 0; i < menuids.length; i++) {
		var ultags = document.getElementById(menuids[i]).getElementsByTagName(
				"ul")
		for ( var t = 0; t < ultags.length; t++) {
			var spanref = document.createElement("span")
			spanref.className = "arrowdiv"
			spanref.innerHTML = "&nbsp;&nbsp;"
			ultags[t].parentNode.getElementsByTagName("a")[0]
					.appendChild(spanref)
			ultags[t].parentNode.onmouseover = function() {
				this.getElementsByTagName("ul")[0].style.left = this.parentNode.offsetWidth
						+ submenuoffset + "px"
				this.getElementsByTagName("ul")[0].style.display = "block"
			}
			ultags[t].parentNode.onmouseout = function() {
				this.getElementsByTagName("ul")[0].style.display = "none"
			}
		}
	}
}

if (window.addEventListener)
	window.addEventListener("load", createcssmenu, false)
else if (window.attachEvent)
	window.attachEvent("onload", createcssmenu)

/*------------------------------------ Ajax Tab ------------------------------------*/
//Promote Content
$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	/* Defining an array with the tab text and AJAX pages: */
	var Tabs = {
		'ภาพสีวาเลนไทน์'	: '/index/load-content/device/promote/type/Valentine-Wallpaper',
		'ธีมโนเกียวาเลนไทน์'	: '/index/load-content/device/promote/type/Valentine-Theme',
		'ทรูโทนวาเลนไทน์'	: '/index/load-content/device/promote/type/Valentine-TrueTone',
		'เกมส์วาเลนไทน์'	: '/index/load-content/device/promote/type/Valentine-Game',
		'ข้อความวาเลนไทน์'	: '/index/load-content/device/promote/type/Valentine-SmaAnimation'
	}
	
	/* Looping through the Tabs object: */
	var z=0;
	$.each(Tabs,function(i,j){
		/* Sequentially creating the tabs and assigning a color from the array: */
		var tmp = $('<li style="width: ' + Math.ceil(718/5) + 'px;"><a href="#" class="tabP">'+i+'</a></li>');
		
		/* Setting the page data for each hyperlink: */
		tmp.find('a').data('page',j);
		
		/* Adding the tab to the UL container: */
		$('ul.promoteTab').append(tmp);
	})

	/* Caching the tabs into a variable for better performance: */
	var the_tabs = $('.tabP');
	
	the_tabs.click(function(e){
		/* "this" points to the clicked tab hyperlink: */
		var element = $(this);

		/* Remove Hilight */
		$('.tabP').removeAttr('style');
		
		/* Hilight Tab */
		element.attr('style', 'background-image: url(/images/head_bluesky.gif)');
		
		/* Checking whether the AJAX fetched page has been cached: */
		if(!element.data('cache'))
		{	
			var fix = '/index/load-content/device/promote/type/Valentine-Wallpaper';
			
			/*-------------- Auto Cache ------------------*/
			if(element.data('page') == fix) {
				$.get(element.data('page'),function(msg){
					$('#promoteContent').html(msg);
					element.data('cache',msg);
				});
			} else {
				/* If no cache is present, show the gif preloader and run an AJAX request: */
				$('#promoteContent').html('<br /><img src="/images/ajax-loader.gif" width="145" height="15" class="preloader" /><br /><br /><span style="color: #F00; font-weight: bolder;">กรุณารอสักครู่</span>');
	
				$.get(element.data('page'),function(msg){
					$('#promoteContent').html(msg);
					
					/* After page was received, add it to the cache for the current hyperlink: */
					element.data('cache',msg);
				});
			}
		}
		else $('#promoteContent').html(element.data('cache'));
		
		//e.preventDefault();
	})

	/* Emulating a click on the first tab so that the content area is not empty: */
	the_tabs.eq(0).click();
});
	
//Mobile Content
$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	/* Defining an array with the tab text and AJAX pages: */
	var Tabs = {
		'ธีมโนเกีย'	: '/index/ajax-load/device/mobile/type/CK',
		'ธีมโนเกีย  เคลื่อนไหว'	: '/index/ajax-load/device/mobile/type/Animation',
		'ธีมโนเกีย  Series 60'	: '/index/ajax-load/device/mobile/type/Series60',
		'ธีมโนเกีย  Serires 40'	: '/index/ajax-load/device/mobile/type/Series40',
		'ธีมโนเกีย  Serires 40 v2'	: '/index/ajax-load/device/mobile/type/Series40V2',
		'ธีมโนเกีย  VDO'	: '/index/ajax-load/device/mobile/type/VDO',
		'ธีมโนเกีย  Flash'	: '/index/ajax-load/device/mobile/type/Flash',
		'ธีม Sony'	: '/index/ajax-load/device/mobile/type/Sony'
	}
	
	/* Looping through the Tabs object: */
	var z=0;
	$.each(Tabs,function(i,j){
		/* Sequentially creating the tabs and assigning a color from the array: */
		var tmp = $('<li style="width: ' + ((718/8)+70) + 'px;"><a href="#" class="tabM">'+i+'</a></li>');
		
		/* Setting the page data for each hyperlink: */
		tmp.find('a').data('page',j);
		
		/* Adding the tab to the UL container: */
		$('ul.mobileTab').append(tmp);
	})

	/* Caching the tabs into a variable for better performance: */
	var the_tabs = $('.tabM');
	
	the_tabs.click(function(e){
		/* "this" points to the clicked tab hyperlink: */
		var element = $(this);

		/* Remove Hilight */
		$('.tabM').removeAttr('style');
		
		/* Hilight Tab */
		element.attr('style', 'background-image: url(/images/head_bluesky.gif)');
		
		/* Checking whether the AJAX fetched page has been cached: */
		if(!element.data('cache'))
		{	
			var fix = '/index/ajax-load/device/mobile/type/CK';
			
			/*-------------- Auto Cache ------------------*/
			if(element.data('page') == fix) {
				$.get(element.data('page'),function(msg){
					$('#mobileContent').html(msg);
					element.data('cache',msg);
				});
			} else {
				/* If no cache is present, show the gif preloader and run an AJAX request: */
				$('#mobileContent').html('<br /><img src="/images/ajax-loader.gif" width="145" height="15" class="preloader" /><br /><br /><span style="color: #F00; font-weight: bolder;">กรุณารอสักครู่</span>');
	
				$.get(element.data('page'),function(msg){
					$('#mobileContent').html(msg);
					
					/* After page was received, add it to the cache for the current hyperlink: */
					element.data('cache',msg);
				});
			}
		}
		else $('#mobileContent').html(element.data('cache'));
		
		e.preventDefault();
	})

	/* Emulating a click on the first tab so that the content area is not empty: */
	the_tabs.eq(0).click();
});

/*------------------------------------ Popup ------------------------------------*/
$(document).ready(function(){
    $("a.linkpopup").click(function(){
        var url = this.href;
        var h = 530;
        var w = 600;
        testwindow = window.open(url, 'popup', 'width=' + w + ',height=' + h + ' status=0, toolbar=0, location=0, menubar=0, resizable=0, scrollbars=0');
        
        var sH = document.documentElement.clientHeight;
        var sW = document.documentElement.clientWidth;
        
        var centerH = (sH - h) / 2;
        var centerW = (sW - w) / 2;
        testwindow.moveTo(centerW, centerH);
        
        return false;
    });
});
