$(document).ready(function(){
	// Init menu (superfish)
	$("ul.nav").superfish();

	// If more than one teaser - cycle them
	if($('.hmrteaser').children().length>1) {
		$('.hmrteaser').cycle({
			timeout: 12000,
			speed: 750,
			random: 1
		});
	}
	
	// INIT OVERLAY
	$("#overlay").overlay({
		closeOnClick: false,
		// start exposing when overlay starts to load
		onBeforeLoad: function() {
			// this line does the magic. it makes the background image sit on top of the mask
			this.getBackgroundImage().expose({color: '#000'});
		}, 
		// when overlay is closed take the expose instance and close it as well
		onClose: function() {
			$.expose.close();
		}
	});
	
	
	// DOWNLOAD DEMOS / DOWNLOAD FORM - BEGIN
	$("button#downloadbutton").click(function(){
		loadDownloadForm();
	});
	// END - DOWNLOAD DEMOS / DOWNLOAD FORM
	
	
	// Screencasts BEGIN
	$(".screencast-thumb-image a").click(function(){
		msg = '';
		msg += '<div class="align-left">';
//		msg += '<div><object width="600" height="450"><param name="movie" value="'+$(this).attr('href')+'"><embed src="'+$(this).attr('href')+'" width="600" height="450"></embed></object></div>';

		msg += '<a href="'+$(this).attr('href')+'" style="display:block; width:600px;height:450px" id="player"></a>';
		msg += '<div>'+$(this).parent().parent().children('.screencast-thumb-description').html()+'</div>';
		msg += '</div>';
		
		$('#overlay_content_inner').html(msg);
		var api = $("#overlay").overlay();
		api.load();
		
		
		flowplayer("player", "fileadmin/templates/main/resources/flowplayer/flowplayer-3.0.7.swf");
		
		return false;
	});
	// END - screencasts
		
	
	
	// TABS BEGIN
	$('#tabs').tabs();
	$("#accordion").accordion({ autoHeight: false });
	
	
    // TABS END


	// Click and erase function no searchfield
	var searchinitvalue = $('#hmr_indexsearch_field').val();
	
	$('#hmr_indexsearch_field').focus(function(){
		if($('#hmr_indexsearch_field').val()==searchinitvalue) {
			$('#hmr_indexsearch_field').val('');
		}
	});
	
	$('#hmr_indexsearch_field').blur(function(){
		if($('#hmr_indexsearch_field').val()=='') {
			$('#hmr_indexsearch_field').val(searchinitvalue);
		}
	});
	// end - click and erase
	
	
	
	
	// LOOK IN URL AND PERFORM CORRESPONDING ACTION
	var strHref = window.location.href;
	
	// If showdownload is in URL, show download overlay
	if(strHref.indexOf("#download")>0) {
		loadDownloadForm();
	}
	
	screencastIndex = strHref.indexOf("#screencast");
	if(screencastIndex>0) {
		$('#screencastslink').click();
		$(strHref.substr(screencastIndex,strHref.length)).click();
	}
	
	
});

function loadDownloadForm() {
	uid = parseInt($('#productuid').html());
	$.post('/index.php?type=143&tx_hmrdrawareproducts_pi3[uid]='+uid,{},function(data){
		$('#overlay_content_inner').html(data);
		var api = $("#overlay").overlay();
		api.load();
	});
}

function loadBuyForm(uid) {
	uid = parseInt(uid);
	$.post('/index.php?type=144&tx_hmrdrawareproducts_pi1[prodid]='+uid,{},function(data){
		$('#overlay_content_inner').html(data);
		var api = $("#overlay").overlay();
		api.load();
	});
}

function postDownloadForm() {

	uid = parseInt($('#productuid').html());
	data = $('#downloaddemoform').serialize();
	$('#overlay_content_inner').html('Sender..');
	
	$.post('/index.php?type=143&tx_hmrdrawareproducts_pi3[uid]='+uid,data,function(data){
		$('#overlay_content_inner').html(data);
	});
	
	return false;
}

function postBuyForm() {

	uid = parseInt($('#prodid').val());
	data = $('#buyproductform').serialize();
	$('#overlay_content_inner').html('Sender..');
	
	$.post('/index.php?type=144&tx_hmrdrawareproducts_pi1[prodid]='+uid,data,function(data){
		$('#overlay_content_inner').html(data);
	});
	
	return false;
}