var cf;
$(document).ready(function () { 
	$('[id=itemtrailer]').hide();
	$('[id^=iteminfo]').hide().find('a[href=#itemtrailer]').click(function() {
		$('.tabs .tab:visible').hide();
		$($(this).attr('href')).show();		
		var the_id = $(this).attr('id').substring(7);
		createTrailer(the_id);
		return false;
	});
	/*
	 * $('.flow .caption a[href^=#]').click(function() { var li = $('.tabbar
	 * li.active') $(li.find('a').attr('href')).hide();
	 * li.removeClass("active"); $($(this).attr('href')).show(); return false;
	 * });
	 */
	$('.titlebar a.back').click(function() { 
		var url = $(this).attr('href'); //alert(url);
		if (url.match(/fetishfilms.eu/)) return true;

		if ($('.tabs .tab:visible').find('.projekktor').length)
		  projekktor($('.tabs .tab:visible').find('.projekktor').attr('id')).setStop();
		$('.tabs .tab:visible').hide(); 
		$('.tabbar li.active').removeClass("active");
		
		$('.tabbar li a[href="' + url + '"]').parent().addClass('active');		
		$(url).show(); 		
		if (url.match(/.shop/)) {
		  $('[id=contentFlow] .flow').height(480);
		  //alert($('[id=contentFlow] .flow').height());
		  cf.resize();
		}
		return false;
	});

	$('#categoriesbutton').click(function() {
		var theLang = $.cookie('language');
		if (!theLang) theLang = 'en';
		var el = $('#categories' + theLang);
		//if (el.is(":visible")) el.hide();
		//else el.show().focus();
		var pos = $(this).position();
		pos.left += 15;
		pos.top += 35;
		el.offset(pos).show().position();
		return false;
	});
	if ($('.catselect').length) $('.catselect').hide().change(function() {
		$(window.location).attr('href', $(this).val());
	}).focusout(function() {//return;
		$(this).hide();
		//$('#categoriesbutton').show();
	});

	$('#quick_find .button').click(function() { $('#quick_find').submit(); });

	var tabs = $('.tabbar');
	tabs.find('li.hidetab').hide();
	var tabsitems = tabs.find('li');
	tabsitems.css({ width:(tabs.width() / (tabsitems.length - 1))});
	
	if (0 && $.browser.mozilla)
	  $('input[name="txtsearch"]').css('height', '32px');
	  
	$('a[href=#search]').click(function() {
		$("input[name='txtsearch']").focus();
		return true;
	});

if ($('#contentFlow').length) {  
  cf = new ContentFlow('contentFlow', {reflectionHeight: 0, circularFlow: false, visibleItems:2, flowSpeedFactor: 2, flowDragFriction: 0, scaleFactorLandscape: "max"});

	$('#contentFlow .item').touchwipe({
		wipeLeft: function() { cf.moveTo('right'); },
		wipeRight: function() { cf.moveTo('left'); },
		min_move_x: 20,
		// preventDefaultEvents: true
	});	

	$('#downloadlink').click(function(){ $(window.location).attr('href', $(this).attr('href')); return false; });
	$('#linkslink').click(function(){ $(window.location).attr('href', $(this).attr('href')); return false; });

	$('#titlebarmain > li').bind('mouseover', menu_open);
	$('#titlebarmain > li').bind('mouseout', menu_timer);
	
     	if ($.browser.msie) projekktor('video', {
	  playerFlashMP4: 'http://shop.fetishfilms.eu/projekktor-V0.9.05/jarisplayer.swf',
	  _platformPriority: ['flash', 'native'],
	});
	else projekktor('video', {
	  playerFlashMP4: 'http://shop.fetishfilms.eu/projekktor-V0.9.05/jarisplayer.swf'
	});
     	
 }
	 
	
});

function createTrailer(the_id) {
//    $('#vplayer').css('height', '').css('width', '');
    var configobj = {
	    "strategy": {"order":["HTML5","Flash"]},
	    "controls": { 'src_img':'/ovp/images/pixel.png'},
	    "players": {
	    	"Flash":{"src":"/ovp/html/ovp.swf","minver":"9"},
	    	"HTML5":{"minver":"0"}
	    }
	};
    ovp.init(configobj);
    var videoconf = {
    	"sources":[{'src':'http://downloads.fetishfilms.eu/trailer/' + the_id + '.mp4', 'type':'video/mp4'}],
    	'posterimg':'http://www.mfxmedia.com/downloadimages/product_thumb.php?img=' + the_id + '.jpg&w=320&h=240',        	
    	'autobuffer':true,
    	'controls':true,
    	'id':'myplayer',
    	'width':'320',
    	'height':'240',
    	'mode': 'overlay',
    	'autoplay': true,
    	'autostart': true,
    	'allowFullScreen': true,
    };
    if ($('#vplayer').length) ovp.render("vplayer",videoconf);
    else ovp.render("myplayer",videoconf);
}

var menutimeout = 500;
var closetimer = 0;
var ddmenuitem = 0;
var ddmenuitem2 = 0;

function menu_open() {
  menu_canceltimer();
  menu_close();
  ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible').show();
  ddmenuitem2 = $(this).find('ul').eq(1).css('visibility', 'visible').show();
}

function menu_close() {	
  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden').hide();
  if(ddmenuitem2) ddmenuitem2.css('visibility', 'hidden').hide();
}

function menu_timer() {	
  closetimer = window.setTimeout(menu_close, menutimeout);
}

function menu_canceltimer() {	
  if(closetimer) {	
    window.clearTimeout(closetimer);
    closetimer = null;
  }
}

document.onclick = menu_close;

function changeCaptions() {
	theCurr = $.cookie('currency');
	theLang = $.cookie('language');
	$('#captions .' + theCurr).show();
	$('#captions .' + theLang).show();
}

function changeLang(theLang) {
	$('.en,.de,.fr,.it').hide();
	$('.' + theLang).show();
	$.cookie('language', theLang, {path:'/'});
}

function changeCurrency(theCurrency) {
	$('.eur,.gbp,.usd,.jpy').hide();
	$('.' + theCurrency).show();
	$.cookie('currency', theCurrency, {path:'/'});
}

function loadLang() {
	var theLang = '';
	if (theLang = $.cookie('language')) changeLang(theLang);
	else changeLang('en');
}
function loadCurrency() {
	var theCurrency = '';
	if (theCurrency = $.cookie('currency')) changeCurrency(theCurrency);
	else changeCurrency('eur');
	$("[name='currency']").val(theCurrency);
}

function changeTime() { //return;
	if (!$('#time_en').length) return;
	$('#time_en').strftime('%d %B %Y');
	$('#time_de').strftime('%d.%m.%Y');
	$('#time_fr').strftime('%d.%m.%Y');
	$('#time_it').strftime('%d.%m.%Y');
}


$(document).ready(function(){
 	changeTime();
	loadLang();
 	loadCurrency();
 	
});


