// url of extension
var baseExtUrl = 'typo3conf/ext/osfo_page/';
var current_page_id = null;

var cur_article = 0;
var count_articles = 4;
var timer;

function auto_scroll(enable) {
	if (enable && !timer)		
		timer = window.setInterval("top_articles_scroll()",5000);	
	else {
		clearInterval(timer)
		timer =null;
	}
}

function top_articles_scroll() {
	if (cur_article>count_articles) 
		cur_article = 0;
	
	$("#top-articles img").addClass('hidden');
	//$("#top-articles img:eq("+cur_article+")").removeClass('hidden');
	$('#' + $("#top-articles a:eq("+cur_article+")").attr('rel')).removeClass('hidden');	
	
	$("#top-articles a").removeClass('overstate');
	$("#top-articles a:eq("+cur_article+")").addClass('overstate');
	
	
	cur_article++;
	
}

function validateEmail(id) {
	var email = $('#'+id).val();
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		alert('Fehlerhafte Eingabe der E-Mail-Adresse.\nEingabe bitte wiederholen.');
		return false;
	} else {
		return true;
	}
}

$(document).ready(function()
{
	//auto_scroll(true);
	count_articles = $("#top-articles > .articles a").size() - 1;

	$("#top-articles .articles a").hover(
		function() {
			//auto_scroll(false);
			$("#top-articles a").removeClass('overstate');
			$(this).addClass('overstate');
			$("#top-articles img").addClass('hidden');
			$('#' + $(this).attr('rel')).removeClass('hidden');
			cur_article = $("#top-articles a").index($(this)) + 1;
		},
		function() {
			//auto_scroll(true);
		}
    );
	

    $("#searchresults").click(function(){
        var searchresult = $("#form_search").attr("name");
        var param = $("#sword").val();
        var href = 'index.php?id='+searchresult+'&no_cache=1&sword='+param;
        window.location.href = href;
        return false;
    });
    
    current_page_id=$("#pageid").val();
    var buy = getURLParam("buy");

    $.getJSON(baseExtUrl+'remote.php',
        {'action':'getProductsInBasket','id':current_page_id, 'buy':buy},
        function(json){
        }   
    );
    
    $('.button-buy').click(function(){
        var pid = $(this).siblings(':hidden').val();
        var variant_id = $("#variant-"+pid).val();
        if ( variant_id == "undefined" ) {
            var variant_id = '';
        }
        var quantity = 1;
        $.getJSON(baseExtUrl+'remote.php',
            {'action':'buyProduct','id':current_page_id,'pid':pid,'variant_id':variant_id,'quantity':quantity},
            function(json){
				if (json.error === 0) 
					alert(json.message);
				else
					alert("Dieses Produkt konnte Ihrem Einkaufswagen nicht hinzugefügt werden.\n Eingabe bitte wiederholen.");
            }
        );
    });
	
	$("input[type=text]").focus(function(){
		this.select();
	});	
	
	// print page button
	$(".print > img").click(function(){window.print();return false;});
});

$(function(){
	$('ul.jd_menu').jdMenu();
	// tak toto nefunguje, takze to menim priamo v javascripte
	$('ul.jd_Menu').jdMenu({disableLinks:false});
	// Add menu hiding on document click
	$(document).bind('click', function() {
		$('ul.jd_menu ul:visible').jdMenuHide();
	});
	
	// correct the width of dropdown menus
	$("ul.mainmenu-second-level").each(function(i){
		var ow = $(this).width();
		$(this).css('width',ow);
	});
	//	$(this).css('min-width',ow);
	//	$(this).children("li").css('width','auto');
	//	$(this).children("li").css('min-width',ow + 8);
	//	//$(this).find("a").css('width',ow);
	//});

	// correct the width of dropdown menus for - IE bug
	//$("ul.mainmenu-second-level > li").each(function(i){
	//	var ow = $(this).parent().width();
	//	$(this).children("a").css('width',ow - 34);
	//});
	
	// if menu item has a submenu, add arrow icon
	$("ul.jd_menu > li > ul > li").each(function(i){
		if( $('ul',this).length > 0){
			$(this).addClass("hasSubmenu");
		}
	});
});

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
} 

function tx_dropdownsitemap_pi1_swapClasses( element )   {
        if( document.getElementById ) {
            var liClass                                  = "tx_dropdownsitemap_pi1_" + element;
            var picture                                  = "pic_" + element;
            document.getElementById( liClass ).className = ( document.getElementById( liClass ).className == "open" ) ? "closed" : "open";
            document.getElementById( picture ).src       = ( document.getElementById( liClass ).className == "open" ) ? "typo3conf/ext/dropdown_sitemap/res/folder-off.gif" : "typo3conf/ext/dropdown_sitemap/res/folder-on.gif";
        }
}
    
var tx_dropdownsitemap_pi1_expanded = 0;
function tx_dropdownsitemap_pi1_expAll()    {
    if( document.getElementsByTagName ) {
        var listItems = document.getElementsByTagName( "li" );
        for( i = 0; i < listItems.length; i++ ) {
            if( listItems[ i ].id.indexOf( "tx_dropdownsitemap_pi1" ) != -1 ) {
                listItems[ i ].className               = ( tx_dropdownsitemap_pi1_expanded ) ? "closed" : "open";
                    var picture                            = "pic_" + listItems[ i ].id.replace( "tx_dropdownsitemap_pi1_", "" );
                        listItems[ i ].className               = ( tx_dropdownsitemap_pi1_expanded ) ? "closed" : "open"
                        document.getElementById( picture ).src = ( tx_dropdownsitemap_pi1_expanded ) ? "typo3conf/ext/dropdown_sitemap/res/folder-on.gif" : "typo3conf/ext/dropdown_sitemap/res/folder-off.gif";
                    }
        }
    document.getElementById( "tx_dropdownsitemap_pi1_expImg" ).src = ( tx_dropdownsitemap_pi1_expanded == 1 ) ? "typo3conf/ext/dropdown_sitemap/res/exp-on.gif" : "typo3conf/ext/dropdown_sitemap/res/exp-off.gif"
    tx_dropdownsitemap_pi1_expanded                                = ( tx_dropdownsitemap_pi1_expanded == 1 ) ? 0 : 1;
    }
}

function checkUncheckAll(checkState, cbGroup)
{
	for (i = 0; i < cbGroup.length; i++)
	{
		if ( cbGroup[i].id == checkState ) {
			cbGroup[i].checked = true;
		} else {
			cbGroup[i].checked = false;
		}
	}
}
            
