if (typeof(neptuny)=="undefined") {
  var neptuny = false;      
}

var CurrentSelIndexDate = 0;
var CurrentSelIndexRating = 0;
var CurrentSelIndexTitle = 0;
var CurrentSelIndexMoviesPerPage = 0;

$(document).ready(function () {
	// load initial content
	
	GetBacktoServer();
			
	$(window).hashchange( function(){
		var re = new RegExp('\\#page=(\\d+)'),
			hash = window.location.hash || '#page=1';

		if ( re.test(hash) ) {
			var m = hash.match(re);
			changePageContents(m[1]);
		}
	})
 
	// Trigger the event (useful on page load).
	$(window).hashchange();	
});

function changePage(pageNo) {
    document.location.hash = "page=" + pageNo;
	changePageContents(pageNo, 1);
}

function changePageContents (pageNo, omni) {
	var oldPageNo = window.__pageNoHash;
	if (oldPageNo == pageNo) {
		return;
	}
	window.__pageNoHash = pageNo;
	
	//set the search combo
	try {
		cmbFieldSearchIdx = getUrlVars()['cmbFieldSearch'];
		$("#FB_cmbFieldSearch_input").val(cmbFieldSearchIdx);
	} catch (e) {}
	
  //load another page
	$.get("filme_list_ajax.aspx?pageNo=" + pageNo + "&r=" + Math.random() ,"", function(data){
		$("#filme_list_content").html(data); 
		afterFilmeListAjax();
		setMovieDetails_Slide();
		attachOver();		
		attachMovieListComboBox();
		attachTooltip();
		attachHandlers();
		if (omni == 1) omnilogic(channelName, pageNo, TotalPageNr);
		setHoverToBox();
	});
}

function changeView(viewT) {
	//load another page
	$.get("filme_list_ajax.aspx?viewT=" + viewT + "&pageNo=1" + "&r=" + Math.random(),"", function(data){
		$("#filme_list_content").html(data); 
		//check if the toolTip is shown
		if($("#tTip").length>0)
			$("#tTip").remove();
		if (viewT==1) { //1 == covers
		    afterFilmeListAjax();
		  	attachOver();
		} else {
	       setMovieDetails_Slide();		
	       attachOver();
		}
		attachHandlers();    
		attachMovieListComboBox();
		attachTooltip();
		setHoverToBox();
	});
}

function changeFilter(filterIndex, whatSelect) {
	filterIndex = parseInt(filterIndex, 10);
	if (whatSelect == 1){
		CurrentSelIndexDate = filterIndex;
	}
	if (whatSelect == 2){
		CurrentSelIndexRating = filterIndex;
	}
	if (whatSelect == 3){
		CurrentSelIndexTitle = filterIndex;
	}
	if (whatSelect == 4){
		CurrentSelIndexMoviesPerPage = filterIndex;
	}
	$.get("filme_list_ajax.aspx?currentSelIndexDate=" + CurrentSelIndexDate + "&currentSelIndexRating=" + CurrentSelIndexRating + "&currentSelIndexTitle=" + CurrentSelIndexTitle + "&currentSelIndexMoviesPerPage=" + CurrentSelIndexMoviesPerPage + "&pageNo=1" + "&pageNo=1" + "&r=" + Math.random(),"", function(data){
		$("#filme_list_content").html(data); 
		afterFilmeListAjax();
        //attachOver();
        setMovieDetails_Slide();		
		attachHandlers();      	
		attachMovieListComboBox();
		attachTooltip();
		setHoverToBox();
	});
}

function afterFilmeListAjax(v) {
 	$(".movieBoxImage").each(function() {
		$(this).easyTooltip({
			useElement: "now_wrapper_" + $(this).attr('id').substring(2)	
    	});
    });           
}

function attachHandlers() {
  $('.mailButton').bind('click', function() {
												dlgTellAfriend(0, "", this);
												$("#smallTooltip").remove();
												$(this).removeClass().addClass('mailButton');
											});
  if(!FlashDetect.installed) {
	     $(".ncheck").attr("href", "#");
	     $(".ncheck").click(function() {show_ng_flash_dialog()});
  }

  $('.filter_left').bind({
		click: function() {
			$(this).addClass("filter_over");
		},
		mouseleave: function() {
			$(this).removeClass("filter_over");
			$(this).find('.options').hide();
			$(this).find('.dropdown').hide();
		}
	});
 }

function attachMovieListComboBox(){		
	//refreshing the dropdown
	if (CurrentSelIndexDate == 1){
		CurrentSelIndexDate = neptuny ? 6 : 5;
	}
	
	$('.selectOptionItems').replaceSelects_ver2();
	$('.selectCategory .options').show();
	$('.selectCategory .options').jScrollPane2({showArrows:true,maintainPosition: false, AutoScroll: false});
	$('.selectCategory .options').hide();
}

function setMovieDetails_Slide() {
	$(".title_t1").click(function() {
		parent__ = $(this).parents('.parent_');
		parent__.find(".movie_details_box_t1").slideToggle("slow", function() { 
			parent__.find(".movie_details_box_t2").slideToggle("slow");	
	});
  });
}

function setHoverToBox() {
    $('.box').hover(function() {
       $(this).css("background-image", "url('themes/basic/images/boxes/recomm.jpg')");
    },
	function() {
	   $(this).css("background-image", "none");
	});
}

function GetBacktoServer(){
	str = window.location.pathname;
    index = str.lastIndexOf('/')
    page = str.substr(index+1,str.length-index);
    data = readCookie('orice');
	if (data == '1'){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()-2);
		document.cookie="orice=1; expires="+exdate.toUTCString() + "; path=/";
    	window.location.reload();
	}
	else{
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+1);
		document.cookie="orice=1; expires="+exdate.toUTCString() + "; path=/";
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	
	return null;
}
