/*
QuiteCMS
 by Zolo Kallay, ZoA
 http://www.zollo.sk
*/


$(window).scroll(function() {
if ( $(window).scrollTop() < $('#Main').offset().top ) {
 $('#Cart').css('top', $('#Main').offset().top - $(window).scrollTop() +'px');
} else {
 $('#Cart').css('top', 0);
};
/*if ( $(window).scrollTop() > $('#Main').offset().top ) {
 $('#Cart').css('top', $(window).scrollTop() +'px');
} else {
 $('#Cart').css('top', $('#Main').offset().top +'px');
};*/
});


$(document).ready(function() {

MenuGroupHover = false;

//$('#Menu span').not( $('#Menu div span') ).first().remove();
//$('#Menu div').each(function() { $(this).children('span').first().remove(); });

$('#Menu div').each(function() {
ThisEq = $('#Menu div').index( $(this) );
$(this).css('left', ( $('#Menu span.MenuGroupLink').eq(ThisEq).position().left -5 ) +'px');
});

$('#Menu a').not( $('#Menu div a') ).hover(function() {
 if ( $(this).closest('span').attr('class') == 'MenuGroupLink' ) {
  MenuGroupEq = $('#Menu span.MenuGroupLink').index( $(this).closest('span') );
  $('div.MenuGroup').not( $('div.MenuGroup').eq( MenuGroupEq ) ).slideUp(200);
  $('div.MenuGroup').eq( MenuGroupEq ).slideDown(200);
 } else {
  $('div.MenuGroup').slideUp(200);
 };
});

$('.MenuGroup').hover(function() { MenuGroupHover = true; }, function() { MenuGroupHover = false; $(this).slideUp(200); });

$('#Menu').hover(function() {}, function() { if (!MenuGroupHover) { $('.MenuGroup').slideUp(200); }; });

$.history.init(pageload);

$('#Menu a').click(function() {

 if ( $(this).closest('span').attr('class') == 'MenuGroupLink' ) { return false; };
  
 $.history.load( $(this).attr('href').slice($(this).attr('href').search(/\Page=/)+5) );
 /*if ( $(this).parents('ul').attr('id') != 'Menu' || $(this).parents('li').children('ul').length == 0) {
  $.history.load( $(this).attr('href').slice($(this).attr('href').search(/\Page=/)+5) );
 } else {
  AFirstHref = $(this).parents('li').children('ul').children('li').children('a:first').attr('href');
  $.history.load( AFirstHref.slice(AFirstHref.search(/\Page=/)+5) );
 };	//if !GroupsContent	*/
 $(this).blur();
 return false;
 
});

})


function pageload(hash) {	//jquery load history

if (!hash) {
 if (document.URL.search(/index.php\?Page=/) != -1) { document.location = document.URL.replace(/index.php\?Page=/, "#"); };
 if (document.URL.search(/index.php\?Lang=/) != -1 && document.URL.search(/&Page=/) != -1) { document.location = document.URL.replace(/&Page=/, "#"); };
 if (document.URL.search(/index.php\?Redir=true&Page=/) != -1) { document.location = document.URL.replace(/index.php\?Redir=true&Page=/, "#"); };
 if (document.URL.search(/index.php\?Redir=true&Lang=/) != -1) { document.location = document.URL.replace(/Redir=true&/, "").replace("&Page=", "#"); };
 hash = StartPage; 
};

$('#Main').prepend("<div style='margin-bottom:10px; font: bold 12px arial'>Lade Inhalt...</div>");

if (hash.match(/^\d+$/)) {	//is numeric
 File = BaseDir + hash +".htm";
} else {	//is string
 if (hash.search(/\?/) != -1) {	//if hash contains GET data
  File = BaseDir + hash.slice(0, hash.search(/\?/)) +".php"+ hash.slice(hash.search(/\?/));
  hash = hash.slice(0, hash.search(/\?/));
 } else {
  File = BaseDir + hash +".php";
 }; 
};

ThisLinkHref = $('#Menu a[href*='+hash+']').attr('href');

$.ajax({ url: File, context: $('#Menu a[href*='+hash+']'), cache: false, success: function(content) {
 if ( $(this).attr('href') == ThisLinkHref ) {	//display the loaded content only if during its loading, another content hasn't been displayed 
  $('#Main').html(content);
  if ( $(window).scrollTop() > $('#Main').offset().top ) {
   $('html, body').animate({scrollTop : $('#Main').offset().top}, 200);
  };  
  $('#Main a').click(function() {
   if ( $(this).attr('href').slice(0, 15) == 'index.php?Page=' || $(this).attr('href').slice(0, 15) == 'index.php?Lang=' ) {
    $.history.load( $(this).attr('href').slice( $(this).attr('href').search(/Page=/)+5 ) );  
    return false;
   };
  });
  if ( $('#Main img').length > 0 && $('#Main img').eq(0).attr('src').search(location.host+"/"+BaseDir) != -1 ) { $('#Main img').each(function() { $(this).attr('src', $(this).attr('src').slice( $(this).attr('src').search(location.host+"/"+BaseDir) + location.host.length+1+BaseDir.length , $(this).attr('src').length ) ); }); };	//webkit img proxy workaround
  MenuHilite(hash);
  quiteview();
  Gallery();
  
  if (hash == 'shop') {
   $('#Cart').css('width', $('#Main').css('width') );
   $('#Cart').css('top', $('#Main').offset().top);
   CartLoad('');
   $('#Main a.CartCountMinusLink').click(function() {
    if ( $("#Main input[name='Count'][rel='"+ $(this).attr('rel') +"']").val() > 1 ) {
     $("#Main input[name='Count'][rel='"+ $(this).attr('rel') +"']").val( parseInt( $("#Main input[name='Count'][rel='"+ $(this).attr('rel') +"']").val() ) -1 );
	};
    return false;
   });
   $('#Main a.CartCountPlusLink').click(function() {
    $("#Main input[name='Count'][rel='"+ $(this).attr('rel') +"']").val( parseInt( $("#Main input[name='Count'][rel='"+ $(this).attr('rel') +"']").val() ) +1 );
    return false;
   });
   $('#Main a.CartAddLink').click(function() {
    CartLoad("?Reference="+ $(this).attr('rel') +"&Count="+ $("#Main input[name='Count'][rel='"+ $(this).attr('rel') +"']").val() );
    return false;
   });
  };
  
 };
}});

}


function CartLoad(Params) {
$('#Cart').load('content/shop-cart.php'+ Params, function() {
 $('#CartDummy').css('height', $('#Cart').css('height') );
 $('#Cart a.CartCountPlusLink').click(function() {
  CartLoad("?Reference="+ $(this).attr('rel') +"&Action=countPlus");
  return false;
 });
 $('#Cart a.CartCountMinusLink').click(function() {
  CartLoad("?Reference="+ $(this).attr('rel') +"&Action=countMinus");
  return false;
 });
 $('#Cart a.CartRemoveLink').click(function() {
  CartLoad("?Reference="+ $(this).attr('rel') +"&Action=remove");
  return false;
 });
 $('#Cart a.CartCheckoutLink').click(function() {
  $.history.load('shop-checkout');
  return false;
 });
});
};


function MainLoad(hash) { $.history.load(hash); };


function MenuHilite(hash) { 
 $('#Menu a').css('color', '');
 //$('#Menu a').css('font-weight','normal');
 $('#Menu a[href*='+hash+']').css('color','#000');
 //$('#Menu a[href*='+hash+']').css('font-weight','bold');
 $('#Menu a[href*='+hash+']').blur();
 
 if ( $('#Menu a[href*='+hash+']').closest('div').attr('class') == 'MenuGroup' ) {
  $('#Menu a[href*='+hash+']').closest('div').show();
  MenuGroupEq = $('#Menu div.MenuGroup').index( $('#Menu a[href*='+hash+']').closest('div') );
  $('#Menu span.MenuGroupLink').eq( MenuGroupEq ).children('a').css('color', '#000');
 };
 
 /*
 $('#Menu a[href*='+hash+']').parents('ul').parents('li').children('a:first').css('color', '#3c0');	//highlight group name
 $('#Menu a[href*='+hash+']').parents('li').children('ul').slideDown(200);
 UlActiveEq = $('#Menu ul').index( $('#Menu a[href*='+hash+']').parents('ul') );
 $('#Menu ul').not('#Menu ul:eq('+ UlActiveEq +')').not( $('#Menu a[href*='+hash+']').parents('li').children('ul') ).slideUp(200); 
 */
}

function Gallery() {
 $('#Gallery h1').click(function() {
 GalleryActive = $("#Gallery ul:eq("+ $('#Gallery h1').index( $(this) ) +")");
 GalleryActive.slideToggle(300);
 });
}


