$(document).ready(function(){


	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
	
	$("ul.jumpbox li strong, ul.jumpbox li span").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});


	$('.default-value').each(function() {
	    var default_value = this.value;
	    $(this).focus(function() {
	        if(this.value == default_value) {
	            this.value = '';
	        }
	    });
	    $(this).blur(function() {
	        if(this.value == '') {
	            this.value = default_value;
	        }
	    });
	});
	
	$('#password-clear').show();
	$('#password-password').hide();
	
	$('#password-clear').focus(function() {
	    $('#password-clear').hide();
	    $('#password-password').show();
	    $('#password-password').focus();
	});
	$('#password-password').blur(function() {
	    if($('#password-password').val() == '') {
	        $('#password-clear').show();
	        $('#password-password').hide();
	    }
	});
	
	// MODELS HOVER
	$(".cam_list").mouseover(function(){
		$(this).addClass("selected");
	});
	$(".cam_list").mouseout(function(){
		$(this).removeClass("selected");
	});
	
	$.viewMap = 
	{
		'M' : $('#M'),
		'F' : $('#F'),
		'S' : $('#S'),
		'P' : $('#P'),
	};

	$('#sex').change(function() 
	{
		// hide all
		$.each($.viewMap, function() { this.hide(); });
		
		// show current
		$.viewMap[$(this).val()].show();
	});
	
	//Default Action tabs
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first-child").addClass("active").show(); //Activate first tab
	$(".tab_content:first-child").show(); //Show first tab content
	
	//Tabs On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
	
	// select change subgenres
	$("select#frm_genre").change(function()
	{
		$.ajax(
		{
			type: "POST",
			url: "/ajax/frm_subgenre",
			data: "id=" + $(this).val(),
			success: function(msg)
			{
				alert(msg);
				$("#frm_subgenre").html(msg);
				$('#frm_subgenre option:first').attr('selected', 'selected');
			}
		});
	})
	
	// change checkboxes
	$('.inp-check', this).jqTransCheckBox();
	
	// change search form
	$('#frm_search').jqTransform({imgPath:'/styles/_default/img/jqtransformplugin/'});
	
	// button click change bg
	$('.btn_tog').mousedown(function()
	{
		$(this).toggleClass("down");
	});
	
	// button hover remove class down
	$('.btn_tog').hover(function()
	{
		$(this).removeClass("down");
	});
	

	
	// check all checkbox cart
	$("#checkAll").click(function()
	{
		var checked_status = this.checked;
		$(".check").each(function()
		{
			this.checked = checked_status;
		});
	});
});

/* image error replace */
function imgError(img, errorImg)
{
	img.src = errorImg;
	img.onerror = '';
	return true;
}


/* change vod subgenres */
function changeSubgenres(genre)
{
	
	$.ajax(
	{
		type: "POST",
		url: "/ajax/frm_subgenre",
		data: "id=0&genre=" + genre,
		success: function(msg)
		{
			$(msg).replaceAll('#frm_subgenre');
			
			if( $('#frm_subgenre', '#frm_search').jqTransSelect().length > 0 )
			{
				//jqTransformAddDocumentListener();
			}
		}
	});
}

/**
* add movie to cart
*/
function addToCart(movie_id)
{
	$.ajax({
		type: "POST",
		url: "/ajax/cart",
		data: "movie_id=" + movie_id,
		success: function(msg)
		{
			$.growl.message(msg);
			$('.save_cart').toggleClass("down");
		}
	});
}

/**
* delete movie from favorites
*/
function deleteFavoriteMovie(movie_id)
{
	$.ajax({
		type: "POST",
		url: "/ajax/delfavoritemovie",
		data: "movie_id=" + movie_id,
		success: function(msg)
		{
			$.growl.message(msg);
			$('#fav' + movie_id ).remove();
			
			var i = 1;
			$('#tab1 .grid_blok').each(function(){
				$(this).removeClass("grid_last");
				if(i % 4 == 0)
				{
					$(this).addClass("grid_last");
				}
				i++;
			});
		}
	});
}

/**
* add cam to favorite
*/

function addLiveFavorite(cam_id)
{
	$.ajax({
		type: "POST",
		url: "/ajax/favorite",
		data: "cam_id=" + cam_id,
		success: function(msg)
		{
			$.growl.message(msg);
			$('.save_cart').toggleClass("down");
		}
	});
}

/**
* delete cam from favorites
*/
function deleteLiveFavorite(cam_id)
{
	$.ajax({
		type: "POST",
		url: "/ajax/delfavorite",
		data: "cam_id=" + cam_id,
		success: function(msg)
		{
			$.growl.message(msg);
			$('#fav' + cam_id ).remove();
			
			var i = 1;
			$('#tab1 .grid_blok').each(function(){
				$(this).removeClass("grid_last");
				if(i % 4 == 0)
				{
					$(this).addClass("grid_last");
				}
				i++;
			});
		}
	});
}


/**
* fill live content
*/
function appendLiveContent(where, start, limit, optionsArr)
{
	var url = '/ajax/appendlivecontent';	
	
	var optionsStr = '';
	
	for(var index in optionsArr)
	{
		optionsStr = optionsStr + '&' + index + '=' + optionsArr[index];
	}
	
	$.ajax({
		type: "POST",
		url: url,
		data: "start=" + start + "&limit=" + limit + optionsStr,
		success: function(msg)
		{
			var id = $('' + where + '');
			id.append(msg);
		}
	});
}

function toggleLiveSearch(type, value, last, page, nav, ref, promo)
{
	var name = $('input[name=' + value + ']');
	
	if(name.is(':checked')) 
	{	
		if(type == 'language')
		{
			if(value == 'languageALL')
			{
				// uncheck languages
				$('.lang').each(function(index)
				{
					$(this).attr('checked', false);
				});
				
				value = 'ALL';
			}
			else
			{
				$('input[name=languageALL]').attr('checked', false);
			}
		}
		else if(type == 'first_language')
		{
			$('input[name=languageALL]').attr('checked', false);	
		}
		else if(type == 'age')
		{
			if(value == 'ageALL')
			{
				// uncheck languages
				$('.age').each(function(index)
				{
					$(this).attr('checked', false);
				});
				
				value = 'ALL';
			}
			else
			{
				$('input[name=ageALL]').attr('checked', false);
			}
		}

		var checked = 'yes';
	}
	else
	{
		var checked = 'no';
	}
	
	if(type == 'language' || type == 'first_language')
	{
		var counter = 0;
		$('.lang:checked').each(function(index)
		{
			counter++;
		});
		
		if(counter == 0)
		{
			$('input[name=languageALL]').attr('checked', true);
		}
		else
		{
			$('input[name=languageALL]').attr('checked', false);
		}
	}
	
	if(type == 'age')
	{
		var counter = 0;
		$('.age:checked').each(function(index)
		{
			counter++;
		});
		
		if(counter == 0)
		{
			$('input[name=ageALL]').attr('checked', true);
		}
		else
		{
			$('input[name=ageALL]').attr('checked', false);
		}
	}

	var url = '/ajax/togglelivesearch';
	
	if($('#tab1').length > 0)
	{
		// get the popular content
		$.ajax({
			type: "POST",
			url: url,
			data: "type=" + type + "&value=" + value + "&checked=" + checked + "&order_by=popular&last=" + last + "&page=" + page + "&nav=" + nav + "&ref=" + ref + "&promo=" + promo,
			success: function(msg)
			{
				if($('#tab1').is(':visible'))
				{
					$('#tab1').fadeOut('slow', function() 
					{
					    $("#tab1").html(msg).fadeIn('slow');
					 });
				}
				else
				{
					$("#tab1").html(msg).hide();
				}			
			}
		});
	}
	
	// get the rating content
	if($('#tab2').length > 0)
	{
		$.ajax({
			type: "POST",
			url: url,
			data: "type=" + type + "&value=" + value + "&checked=" + checked + "&order_by=rating&last=" + last + "&ref=" + ref + "&promo=" + promo,
			success: function(msg)
			{
				if($('#tab2').is(':visible'))
				{
					$('#tab2').fadeOut('slow', function() 
					{
					    $("#tab2").html(msg).fadeIn('slow');
					 });
				}
				else
				{
					$("#tab2").html(msg).hide();
				}
			}
		});
	}
	
	// get the date content
	if($('#tab3').length > 0)
	{
		$.ajax({
			type: "POST",
			url: url,
			data: "type=" + type + "&value=" + value + "&checked=" + checked + "&order_by=date&last=" + last + "&ref=" + ref + "&promo=" + promo,
			success: function(msg)
			{
				if($('#tab3').is(':visible'))
				{
					$('#tab3').fadeOut('slow', function() 
					{
					    $("#tab3").html(msg).fadeIn('slow');
					 });
				}
				else
				{
					$("#tab3").html(msg).hide();
				}
			}
		});
	}
}


function keepAlive()
{
	$.ajax(
	{
		type: "POST",
		url: "/ajax/keepalive/",
		data: "alive=yes",
		success: function(time)
		{
			if(time > 0)
			{
				setTimeout('keepAlive()', time);
			}
		}
	});
}

function getSnapshots(month, cam_id)
{
	$.ajax(
	{
		type: "POST",
		url: "/ajax/snapshots/",
		data: "month=" + month + "&cam_id=" + cam_id,
		success: function(shots)
		{
			if(shots != 'error')
			{
				$("#shots").fadeOut('slow', function() {
			    	$("#shots").html(shots).fadeIn('slow');			    
			  	});
			}
		}
	});
}
