jQuery.ajaxSetup({'cache' : false});

$(function() {

	var url = window.location.href;
	
    $('input[type=text], input[type=password], textarea').each(function(){			
	    var d_value = $(this).attr('title');			    
	    $(this).focus(function(){
	        if(this.value == d_value){
	            this.value = '';
	        }
	    });	
				    
	    $(this).blur(function(){
	    	if(this.value == '' && d_value.length > 2){
	            this.value = d_value;
	        }
	    });		    
	});
	
	$('#nav-tv').click(function(){
		window.location.href = $('.tv-nav .movies a').attr('href');
	});
		
	$('#nav-people').click(function(){
		window.location.href = $('.people-nav .interviews a').attr('href');
	});
	
	function refresh_date_height(){
		$('.movies_sidebar_boxes li dl dd').each(function(){
			$('var',this).css('height',$(this).height()+'px');
		});
	}
	
	$('.sport_btn').click(function(){
		$('.sport_btn').removeClass('active');
		$(this).addClass('active');
		$('.movies_sidebar_boxes').hide();
		var id = $(this).attr('lang');
		$('.movies_sidebar_boxes#'+id).fadeIn('slow');
	});
	
	refresh_date_height();

	$('.movies_sidebar_boxes li p').click(function(){
		if(!$(this).parent().hasClass('active')){

			var ul_object = $(this).parent().parent();
			
			var weekDay = $(this).attr('lang');
			var section = $(this).parent().attr('lang');
			var e = this;
			
			$.post('items.php',{weekDay:weekDay,section:section},function(rez){
				var before_active_element = $('li.active',ul_object).index();
				$('li.active div.movies_nav',ul_object).remove();
				$('dl',$(e).parent()).html(rez.list).hide();
				$(e).parent().append(rez.nav);
				
				$('li.active div.movies_nav',ul_object).remove();			
				$(e).parent().addClass('active');

				
				$('li.active dl',ul_object).slideUp('medium',function(){
					$('li:eq('+ before_active_element +')',ul_object).removeClass('active');
				});

				$('dl',$(e).parent()).slideDown('slow',function(){
					$('li.active div.movies_nav',ul_object).fadeIn('medium');
				});
				
				refresh_date_height();
			},'json');
		}else{
			$('dl',$(this).parent()).slideUp('medium');
			$('li.active div.movies_nav',$(this).parent().parent()).remove();
			$(this).parent().removeClass('active');
		}
	});
	
	$('.side_two_box .nav').click(function(){
		var id = $(this).attr('lang');
		$('.side_two_box .nav').removeClass('active');
		$(this).addClass('active');
		$('.side_two_box ul').hide();
		$('.side_two_box ul[id="'+ id +'"]').fadeIn('slow');
	});
	
	if($('#filter_name').text() == 'tvs'){
		$('#nav-tvs').click();
		
		var val = $('#filter_value').text();
		$('#filters-tvs-list li a').each(function(){
			if($(this).attr('lang') == val){
				$(this).addClass('active');
			}
		});
	}	
	
	$('.settings-manage .delete').click(function(){
		var title = $('.title',$(this).parent().parent()).text();
		if(!confirm('Наистина ли искате да изтриете '+ title)){
			return false;
		}
	});
	
	if($('#filter_name').text() == 'programs'){
		$('#nav-programs').click();
		
		var val = $('#filter_value').text();
		$('#filters-programs-list li a').each(function(){
			if($(this).attr('lang') == val){
				$(this).addClass('active');
			}
		});
	}

	$('.movie_next').live('click',function(){
		$(this).addClass('movie_back').removeClass('movie_next').text('По-малко ...');
		$('dl dd.toggle',$(this).parent().parent()).fadeIn('slow');
	});
		
	$('.movie_back').live('click',function(){
		$(this).addClass('movie_next').removeClass('movie_back').text('Повече ...');;
		$('dl dd.toggle',$(this).parent().parent()).fadeOut('fast');
	});
	
	$('#get_choise').click(function(){
		var href = $(this).attr('lang');

		var ids = new Array();
		var i = 0;
		$('#programs_choise ul li input').each(function(){
			var checked = $(this).attr('checked');
			
			if(checked){
				ids[i] = $(this).val();
				i++;
			}
		});
		
		window.location.href = href+ids.join('.');
		return false;
	});
	
	$('.carousel_images li a').click(function(){
		var src = $('img',this).attr('src');
		$('.carousel .image-holder img').attr('src',src);
		
		$('.carousel_images li').removeClass('active');
		$(this).parent().addClass('active');
	});
	
	if($('#program-all').length || $('#search-result').length){
		$('.sidebar').height($('#inner_content').height()-23+'px');
	}
	
//	var selected_menu = $('#navigation').attr('class');
//	$('#navigation ul li.'+selected_menu).addClass('active');
	
	$('#program-all li').height($('#program-all').height()+'px');

	$('#programs_choise ul li').click(function(){
		var val = $('input',this).attr('checked');
		
		if(val == true){
			$('input',this).attr('checked',false);
		}else{
			$('input',this).attr('checked',true);
		}
	});
	$('#programs_choise ul li input').click(function(){
		var val = $(this).attr('checked');
		
		if(val == true){
			$(this).attr('checked',false);
		}else{
			$(this).attr('checked',true);
		}
	});
	
	function calculate_programs_carousel_scroll(click){
		var count = $('#program-all li').length;
		
		var single_size = 165;
		
		var all_size = (count*single_size)-(4*single_size);
		
		var scroll_size = (4*single_size)*click;
		
		var scroll = 0;
		
		if(scroll_size > all_size){
			scroll = all_size;
		}else{
			scroll = scroll_size;
		}
		
		return scroll;
	}
	
	var click = 0;
	
	$('#programs_carousel_next').click(function(){
		var all = $('#program-all li').length;
		
		if(click != parseInt(all/4)){
			click++;
		}

		if(click > 0){$('#programs_carousel_back').show();}			
		if(click == parseInt(all/4)-1){$(this).hide();}
		
		var scroll = calculate_programs_carousel_scroll(click);
		
		var eq = ((click*4)*2)-1;
		if(eq > all){
			eq = all-1;
		}

		$('#program-all li').removeClass('last');
		$('#program-all li:eq('+ eq +')').addClass('last');
		
		$('#program-all').stop().animate({	
		    left: '-'+scroll+'px'
		}, 1000);
	});
	
	$('#programs_carousel_back').click(function(){		
		var all = $('#program-all li').length;
		
		if(click != 0){
			click--;
		}
		
		if(click != parseInt(all/4)){$('#programs_carousel_next').show();}
		if(click == 0){$(this).hide();}
		
		var scroll = calculate_programs_carousel_scroll(click);

		var eq = ((click*4)*2)-1;
		if(click == 0){
			eq = 3;
		}
		
		$('#program-all').stop().animate({	
		    left: '-'+scroll+'px'
		}, 1000,function(){
			$('#program-all li').removeClass('last');
			$('#program-all li:eq('+ eq +')').addClass('last');
		});
	});	
	

	$(".carousel div,.carousel img").click(function(){
		var data = $(this).attr('class').replace(' active','');
		location.href = $('.carousel img.'+data).attr('longdesc');
	});

	$(".carousel .nav li a").mouseover(function(e){
		var search = $(this).attr("href").replace("#","");
		
		if(e.clientX){
			int=window.clearInterval(int);
		}
		
		if(!$(".carousel .image-holder img."+search).hasClass('active')){
		
			$(".carousel .nav li").removeClass("active");
			$(this).parent().addClass("active");
			
			$(".carousel div").removeClass("active");
			$(".carousel div."+search).addClass("active");
			
			$(".carousel img").attr("style","");
			$(".carousel img.before").removeClass("before");
			$(".carousel img.active").addClass("before").removeClass("active");
			$(".carousel img."+search).addClass("active").hide().fadeIn("slow");
		}
	});
	
	var int = setInterval('change_carousel();',3000);

	$('.scroll-pane').jScrollPane({showArrows: true});

});
	
function change_carousel(){
	if($(".carousel .nav .active").index() == 3){
		$(".carousel .nav li:eq(0) a").mouseover();
	}else{
		$('a',$(".carousel .nav .active").next()).mouseover();
	}
}

function get_url_params(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

	for(var i = 0; i < hashes.length; i++){

		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}



function select(selector,hidden,overflow){
	
	//var drop_width = $(selector + ' li:last').width();
	//$(selector + ' .selected').css('width',drop_width +'px');
	
	$(selector + ' .selected').click(function(){
		
		// close before opened drop downs
		var id = selector.replace('#','');
		$('ul').each(function(){
			if($(this).attr('id') != id){
				$('li',this).each(function(){
					if($(this).hasClass('active')){
						$(this).removeClass('active');
						$(this).addClass('hide');
					}
				});
				$('.selected',this).attr('style',null);
				$('.hide',this).attr('style',null);
				$(this).attr('style',null);
			}
		});	
		// end
				
		$(this).parent().attr('style','z-index:10000;');
				
		if(overflow == true){
			$(selector).attr('style','overflow:scroll;overflow-x:hidden;height:140px;z-index:10000;');
		}
		
		// set separator
		//$(this).attr('style','border-bottom:1px solid #5d5d5d;');
		// end
		$(selector + ' .hide').removeClass('selected_active');
		
		// show drop down
		$('.hide',$(this).parent()).each(function() {
			$(this).removeClass('hide').addClass('active');
			
			// check if has selected and mark up 
			if($(selector + ' .selected').attr('lang') == $(this).attr('id')){
				$(this).addClass('selected_active');
			}
			// end
		});
		// end
		
		// animate on show
		$(selector + ' .active').hide().fadeIn('fast');
		// end
		
		// set selected params on click
		$(selector + ' .active').click(function (){
			var val = $(this).attr('id');
			$(selector + ' .selected').html($(this).text());
			$(hidden).val(val); 
			var url = window.location.href;
			var get_url = get_url_params();
			
			$(selector + ' .selected').attr('lang',val)
			
			// on complete hide drop down
			$(selector + ' .active').each(function(){
				$(this).removeClass('active');
				$(this).addClass('hide');
				$(selector).attr('style',null);
				$(selector + ' .hide').attr('style',null);
			});
			// end
			
			// remove bottom border
			$(selector + ' .selected').attr('style',null);
			// end
			
			if(strpos(url,'sort=') !== false){
				url = url.replace('sort='+get_url['sort'],'sort='+ val);
				window.location.href = url;
			}else{
				if(strpos(url,'?') !== false){var sign = '&';}else{var sign = '?';}
				window.location.href = url + sign +'sort=' + val;
			}
		});
		// end
		
		// hover animation
		$(selector + ' .active').hover(function(){
			$(this).css('background','#2a2a2a');
		},function(){
			$(this).attr('style',null);
		});
		// end
	});
	
	// close drop downs if click on body
	$('body').click(function(e){
		if(e.target.id != $(selector + ' .selected').attr('id')){
			$(selector + ' .active').each(function(){
				$(this).removeClass('active');
				$(this).addClass('hide');
			});
			$(selector + ' .selected').attr('style',null);
			$(selector + ' .hide').attr('style',null);
			$(selector).attr('style',null);
		}
	});// end
}

function select_from_options(selector,hidden){
	var url = get_url_params();
	var value = url['sort'];

	if(value != null && value != ''){
		$(selector+" .hide").each(function(){
			var success = false;

			if($(this).attr('id') == value){
				success = true;
			}
			
			if(success == true){
				$(this).addClass('selected_active');
				$('.selected',$(this).parent()).attr('lang',$(this).attr('id')).html($(this).text());
				$(hidden).val($(this).attr('id'));
			}
		});	
	}
}

function strpos (haystack, needle, offset) {
    var i = (haystack + '').indexOf(needle, (offset || 0));
    return i === -1 ? false : i;
}
function rtrim (str, charlist) {
    charlist = !charlist ? ' \\s\u00A0' : (charlist + '').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\\$1');
    var re = new RegExp('[' + charlist + ']+$', 'g');    return (str + '').replace(re, '');
}
