
function initCycle( viewSelector, navSelector )
{	
	function changeTab()
	{
		var index = this.id.split("_")[1] || 0;
		
		$( navSelector ).removeClass("active");
		$( navSelector + ":eq(" + index + ")" ).addClass("active");				
	}
	
	$(
		function()
		{
			$( viewSelector ).cycle(

				{
					timeout: 5000,
					speed: 1000,
					before: changeTab
				}
			);
		}
	);
}

function initNav( swf, id, w, h, flashvars )
{	
	if ( document.getElementById( id ) )
	{
		var flashvars = flashvars || {};
		
		var params = 
		{
			allowfullscreen: "true",
			allowscriptaccess: "always", 
			wmode: "transparent"
		};
		
		var attributes = {};
				
		swfobject.embedSWF( swf, id, w, h, "9.0.0", "/static/swf/expressInstall.swf", flashvars, params, attributes );
	}
}



function initCarousel()
{
	initCycle( '.carousel-container', ".carousel-navigation a" );
	
	var numItems = $(".carousel-navigation li").size() - 1;
	var x = $(".carousel-navigation").width() / 2;
	var dist = 10;
	
	var sx = parseInt( x - ( numItems * dist ) / 2, 10 );
	
	$('.carousel-navigation li').each(
		function(i)
		{
			var left = sx + (i * dist);
			
			$(this).css( { "left": left } );
		}
	);
}

function initFilter()
{ 
	$('.filter select').change(
		function() {
			
			var page = $('.filter').attr("title");
					
			if( page == 'exclusive') 
			{
				var selectedSerie = $('#serie').val();
				var selectedSort = $('#sort').val();		
			
				document.location.href = '/page/exclusive/p/1/' + selectedSerie + '/' + selectedSort;
				
			} else if (page == 'friends-of-mtv') {
				
				var selectedCategory = $('#category').val();
				var selectedSort = $('#sort').val();		

				document.location.href = '/page/friends-of-mtv/p/1/' + selectedCategory + '/' + selectedSort;
				
			} else if (page == 'agenda') {
				
				var selectedYear = $('#year').val();
				var selectedMonth = $('#month').val();		

				document.location.href = '/page/agenda/p/1/' + selectedYear + '/' + selectedMonth;
			
			} else {
			
				var selectedSort = $('#sort').val();		
			
				document.location.href = '/page/' + page + '/p/1/' + selectedSort;
			}
		}			
	);
}

function initUserMobile() {
	
	$("input[name='mobile']").click(function() { 
		if($(this).val() == '06-nummer') {
			$(this).val('');		
		}
	});
	
	$(".login input[name='mobile']").blur(function() {
		if($(this).val() == '') {
			$(this).val('06-nummer');	
		}
	});

	$(".login input[name='password']").click(function() { 
		$(this).val('');										 
	});										 
	
	$("input[name='mobile']").keyup(function() {	
			filterUserMobile(this);
	}); 
	
	$("input[name='wap-mobile']").keyup(function() {	
			filterUserMobile(this);
	}); 
}

function filterUserMobile(obj) {
				
	var mobile = $(obj).val();
	
	if(mobile.substr(0,4) == '0031') {							
		$(obj).val('06' + mobile.substr(5, mobile.length));	
	}
	if(mobile.substr(0,3) == '+31') {							
		$(obj).val('06' + mobile.substr(4, mobile.length));	
	}
	
	if(mobile.substr(0,3) == '06-') {							
		$(obj).val('06' + mobile.substr(4, mobile.length));	
	}
}

// huidige url inclusief /page/
function baseURL() {
	var getURL = location.href;
	var getURL = getURL.split('/');
	var getURL = getURL[0]+'//'+getURL[2]+'/'+getURL[3]+'/';
	return getURL;
}



function submitWapForm() {

	var wapmobile = $('#wap-mobile').val();

	$('.wapsuccess').remove();
	$('.waperror').remove();

	$.ajax({
			type:		"POST",
			url:		baseURL() + "ajax/sendwap/",
			data:		{wapmobile:wapmobile},
			dataType:	"data",

			success: function(msg) {
				
				if(msg == 'OK') {

					$('#div_wapmessage').html("<p class=\"wapsuccess\">Het WAP bericht is verstuurd</p>");
					$('.wap-form').hide();
					
				}
				else {
					$('#div_wapmessage').html("<p class=\"waperror\">" + msg + "</p>");
				}
			},
			beforeSend: function(msg) {
				$('#div_wapmessage').html("<img id=\"preloader\" src=\"/static/images/preloader.gif\">");
			}
			
		});
	
	
	return false;

}

function initExternalLinks()
{	
	$("a[rel='external']").attr( { target: "_blank" } ).each( function() {
		
		var str = $(this).html();
		
		if(str.indexOf('http://') == 0) {
			var newstr = $(this).html().slice(7);
			$(this).html(newstr); 
		}
		
	});
}

function initDialogs(){
	
	$('#wapdialog').jqm({modal:true, trigger: 'a.wapdialog'});
	$('#nosmsreceiveddialog').jqm({modal:true, trigger: 'a.nosmsreceiveddialog'});	
	$('#noemailreceiveddialog').jqm({modal:true, trigger: 'a.noemailreceiveddialog'});	
	$('#contactdialog').jqm({modal:true, trigger: 'a.contactdialog'});	
}

function initLightbox()
{
	$(".gallery-content a").lightBox(
	
	
	);
}

function mtvEmbed(embedType, file, width, height, idContainer, idPlayer, autoStart)
{
	if (embedType == 'wmv') {
		var clsID       = 'CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95';
		var mimeType    = 'application/x-oleobject';
		var codeBase    = 'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112';
		var sourceParam = 'filename';
		var pluginPage  = '';
	} else if (embedType == 'mov' || embedType == 'mp4') {
		var clsID       = 'CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B';
		var mimeType    = 'image/x-macpaint';
		var codeBase    = 'http://www.apple.com/qtactivex/qtplugin.cab';
		var sourceParam = 'src';
		var pluginPage  = 'pluginpage="http://www.apple.com/quicktime/download"';
	} else {
		return false;
	}

	var openObject = '<object id="' + idPlayer + '" width="' + width + '" height="' + height + '" classid="' + clsID + '" type="' + mimeType + '" codebase="' + codeBase + '">';
	var closeObject = '</object>';
	
	var param1 = '<param name="' + sourceParam + '" value="' + file + '">';
	var param2 = '<param name="Showcontrols" value="True">';
	var param3 = '<param name="autoStart" value="' + autoStart + '">';
	
	var embed = '<embed type="' + mimeType + '" src="' + file + '" name="' + idPlayer + '" width="' + width + '" height="' + height + '" ' + pluginPage +' ></embed>';

	var content = openObject + param1 + param2 + param3 + embed + closeObject;

	$('#' + idContainer).append(content);
}

$(document).ready(
	
	function()
	{
		initCarousel();
		initUserMobile();
		initExternalLinks();
		// met delay ivm sifr
		setTimeout( function() { initCycle( '.mymtv-carousel .views', ".mymtv-carousel-navigation a" ); }, 4000 );
				
		$("#ticker").liScroll();

		initFilter();
		
		initDialogs();
		
		initLightbox();
	}
	
	
);