var $ = jQuery.noConflict();
$(function()	{
	$('#lang_layer').hide();
	$('#lang_box').click(function()	{
		if($('#lang_layer').is(':hidden')) {
			$('#lang_layer').fadeIn(200);
			
		}else{
			$('#lang_layer').fadeOut(200);
		}
	});
	$('#lang_layer').hover( function() {},
  		function() { $('#lang_layer').fadeOut(200);
    });

	$('#search_query').click(function() {
		$('#search_query').val('');
	});
	$('#btn_basket').click(function(){
		location.href='shopping_cart.php';
	});

	$('.layer_cart').hide();
	$('.az_cart').click(function() {
		$('#loading').remove();
		$('.layer_news').hide();
		if($('.layer_cart').is(':hidden')) {
			$('.layer_cart').fadeIn(200);
			$('#cart_content').append('<div style="width:220px" id="loading"><br><br><br><center><img src="img/loading.gif"></center></div>');
			$.ajax({
				url: 'get_cart.php',
				contentType: 'text/html; charset=UTF-8',
				type: 'GET',
				data: 'repri_cart=9',
				success: function(data) {
					$('#loading').remove();
					$('#cust_cart_info').remove();
					$('#cart_content').append('<div id="cust_cart_info">'+ data +'</div>');
				}
			});
		} else {
			$('.layer_cart').fadeOut(200);
		}
	});
	$('.layer_cart').hover( function() {}, function() { $('.layer_cart').fadeOut(200); });
	
	
	$('.layer_news').hide();
	$('.az_news').click(function() {
		$('#loading').remove();
		$('.layer_cart').hide();
		if($('.layer_news').is(':hidden')) {
			$('.layer_news').fadeIn(200);
			
			$.ajax({
				url: 'get_cart.php',
				contentType: 'text/html; charset=UTF-8',
				type: 'GET',
				data: 'repri_cart=9',
				success: function(data) {
					
				}
			});
		} else {
			$('.layer_news').fadeOut(200);
		}
	});
	$('.layer_news').hover( function() {}, function() { $('.layer_news').fadeOut(200); });

});







function change_pic (tmp_node, highRes) {
	var highRes = (highRes == null) ? false : highRes;

	if(highRes == true)
		var neu_src = $('#'+tmp_node).attr("alt");
	else
		var neu_src = $('#'+tmp_node).attr("src"); 

	var neu_title = $('#'+tmp_node).attr("title");
	var neu_alt = $('#'+tmp_node).attr("alt");

  var img = new Image();
	$.each($('#changePic a'),function() {
				$(this).remove();
			});
	$.each($('#changePic img'),function() {
				$(this).remove();
			});
	$('#changePic').addClass('loading');;
  $(img).load(function () {
      $(this).hide();
      $(this).attr('id','mainpic_small');
      $(this).attr('alt',neu_title);
      $(this).css('width','auto');
      $(this).css('height','auto');
      $('#changePic').removeClass('loading').append(this);
      $(this).fadeIn('800');
      $(this).css('display','inline');
  }).error(function () {
      // notify the user that the image could not be loaded
  }).attr('src', neu_src);
};




$(function()	{
	$('#cust_login').hide();
	$('#a_account').click(function()	{
		if($('#cust_login').is(':hidden')) {
			$('#cust_login').fadeIn(200);
			$('#newsletter_td inputa').click(function() {
				$('#cust_login_info').append('<div style="width:220px" id="loading"><br><br><br><center><img src="img/loading.gif"></center></div>');

				$.ajax({
					url: 'newsletter_ajax.php',
					type: 'POST',
					data: 'email=' + $('#newsletter_email').val(),
					success: function(data){
						$('#loading').remove();
						$('#sign_in').remove();
						$('#cust_login_info').append('<img src="img/r_okay.png">');
					}
				});
				return false;
			});
		}else{
			$('#cust_login').fadeOut(200);
		}
	});
	$('#cust_login').hover( function() {},
  		function() { $('#cust_login').fadeOut(200);
    });
});



function PressGrow(node,id) {
	if(node == "down"){ $('#'+id).removeClass('noActive'); } else { $('#'+id).addClass('noActive'); }
}



function openPopup(link){
	myWin = window.open(link, "Popup", "toolbar=0, scrollbars=yes, resizable=yes, height=400, width=600");
  myWin.focus();

}

function download_pdf(){
	var myPDF = $('#get_PDF').val();
	if(myPDF != '1x1'){
		window.open('dl_pdf.php?xfile='+myPDF);
		//console.log(myPDF);
	}
	
}

function getcountry(node) {
	if(node=="europe") { countr_id = 3; }
	if(node=="africa") { countr_id = 4; }
	if(node=="america") { countr_id = 1; }
	if(node=="asia") { countr_id = 5; }
	if(node=="australia") { countr_id = 6; }
	d_mouseover(true);
	$.ajax({
		url: 'mydealer.php',
		contentType: 'text/html; charset=utf-8',
		type: 'GET',
		data: 'mode=getCountries&cont_id='+countr_id,
		success: function (data) {
			$('.wm_content').remove();
			$('.wm_countries_content').remove();
			$('.wm_countries').append('<div class="wm_countries_content">'+data+'</div>').fadeIn(200);
			//$('.wm_countries').fadeIn(200);
		}
	});	
}

function getdealers(node) {
	set_active(node);
	$.ajax({
		url: 'mydealer.php',
		contentType: 'text/html; charset=utf-8',
		type: 'GET',
		data: 'mode=getDealers&land_id='+node,
		success: function (data) {
			$('.wm_content').remove();
			$('.wm_layer').append('<div class="wm_content">'+data+'</div>').fadeIn(200);
			//$('.wm_content').fadeIn(200);
		}
	});	
}

function set_active(node_a){
	$('.wm_countries_content').children('a').each( function() {
				if($(this).attr('id') == node_a)
					$(this).addClass('active');
				else
					$(this).removeClass('active');
			});
}

function d_mouseover(disable){
	if(disable){
	$.each($('.wm_area'),function()
			{
				this.onmouseover = '';
				this.onclick = '';
			});
	}
	else {
	$.each($('.wm_area'),function()
			{
				this.onmouseover = function(){wm_change(this.id)};
				this.onclick = function(){getcountry(this.id);return false;};
			});
	}

	
}

function wm_change(node) {
	$('.wm_layer').removeClass('america');
	$('.wm_layer').removeClass('asia');
	$('.wm_layer').removeClass('australia');
	$('.wm_layer').removeClass('africa');
	$('.wm_layer').removeClass('europe');
	$('.wm_layer').addClass(node);
}
function wm_remove() {
	$('.wm_countries_content').remove();
	$('.wm_countries').fadeOut(200);
	d_mouseover(false);
}

function load_ps() {
		$('#content_info').remove();
		$('#container_sl').remove();
		$('#product_box').remove();
		$('#content').append('<div class="grid_12" id="loading"><br><br><br><center><img src="img/loading.gif"></center></div>');
		
		$.ajax({
			url: 'get_gategories.php',
			contentType: 'text/html; charset=utf-8',
			type: 'GET',
			data: 'cat_id=0&cat_type1=img',
			success: function(data){
				$('#loading').fadeOut(200);
				$('#loading').remove();
				$('#content').append('<div id="content_info"></div>');
				$('#content_info').append(data);
			}
		});
		
		return false;
}


function changeDealerCountry(id){
	$('.change .active').fadeOut('slow', function(){
		$('.change .active').removeClass('active');
		$('#ac_'+id).animate({opacity: '+=0'}, 300, function(){
			$('#ac_'+id).fadeIn('slow').addClass('active')})
	});
}


$(function()	{
	$('#lnk_products').click(function(){
		open_products();
		return false;
	});
	$('#home_intro').click(function(){
		$('#lnk_products').click();
	});
	
	$('.scrollable').scrollable({
			vertical:true, 
			items:'.item',
			next:'.next',
			prev:'.prev',
			speed:800,
			easing:'swing',
			circular:false
		});

	$('#quick_find').submit(function(){
		if (($('#search_query').val() == 'Search') || ($('#search_query').val() == 'Suche') || ($('#search_query').val() == 'Recherche') || ($('#search_query').val() == 'Busca')){
			$('#search_query').val('');
			$('#quick_find').submit();
			return false;
		}
	});
});


function open_products(){
		$('#sportclip').remove();
		$('#content_info').remove();
		$('#container_sl').remove();
		$('#product_box').remove();
		$('#starnote').remove();
		$('#content').append('<div class="grid_12" id="loading"><br><br><br><center><img src="img/loading.gif"></center></div>');
		
		$.ajax({
			url: 'get_gategories.php',
			contentType: 'text/html; charset=utf-8',
			type: 'GET',
			data: 'cat_id=0&cat_type1=img',
			success: function(data){
				$('#loading').fadeOut(200);
				$('#loading').remove();
				$('#content').append('<div id="content_info"></div>');
				$('#content_info').append(data);
				$('#content_info').append('<br style="clear:both; />');
				$('#lnk_products').addClass('active');
				$('#lnk_support, #lnk_rest, #lnk_fach, #lnk_comp, #lnk_press, #box_footer li a').removeClass('active');
			}
		});
		
		return false;
}

function open_cat(id)	{
	
	
	if($('#loading')){
		$('#loading').remove();
	}
	$('#wech').remove();
	$('#content').append('<div class="grid_12" id="loading"><center><img src="img/loading.gif"></center></div>');
	$('#container_sl').remove();
	$('#product_box').remove();
	$('#content_info div').animate({"height": 66}, {duration: "slow" });
	$('#content_info').animate({"height": 66}, {duration: "slow" });
	var nav_trail = 0;
	if(id == 2 || id == 4 || id == 304 || id == 311) {
		open_sub_cat(id);
		nav_trail = 1;
	window.setTimeout('high_cat('+id+')', 400);
	} else {
		
			$.ajax({
				url: 'get_gategories.php',
				contentType: 'text/html; charset=UTF-8',
				type: 'GET',
				data: 'cat_id='+ id +'&cat_type1=slider',
				success: function(data){
					$('#loading').fadeOut(200);
					$('#loading').remove();
					$('#tmp_cInfo').remove();
					$('#content').append('<br id="wech" style="clear:both;line-height:2px;" /><div id="container_sl"><div class="sliderGallery sliderGallery_Cat_'+id+'"></div><br style="clear:both;" /></div>');
					$('.sliderGallery').append('<div class="left_'+id+'" /><ul class="imageLibrary">'+ data + '</ul><div class="slide_contain"><div class="slider ui-slider"><a class="handle ui-slider-handle"></a></div></div><div class="right_'+id+'" />');
					$('#container_sl').fadeIn(200, function(){high_cat(id)});
					load_slide();
				}
			});
				
	}
	if(nav_trail == 1){
		return true;
	}else{
		return false;
	}
	
}

function show_cat(id){
	if(id != 311){
		open_products();
		$('#content').animate({opacity: '+=0'}, 800, function(){open_cat(id)});
	}	else{

		open_cat(311);		
		$('#content_info').remove();
		$('#lnk_products').removeClass('active');
		$('#lnk_rest').addClass('active');

	}
}


function high_cat(high){
	$('#content_info div img').hide();
	show = '#high_'+high;
	$(show).fadeIn(1000);
}




function open_sub_cat(cat_id, detailview ) {
	var detailview = (detailview == null) ? false : detailview;
	
	if(!detailview){
		var appe = '#content';
	}
	else
		var appe = '#detailNav';

	if($('#loading')){
		$('#loading').remove();
	}
	$.each($('.goAway'),function() {
				$(this).remove();
			});
	$(appe).append('<div style="width:800px" id="loading"><center><img src="img/loading.gif" /></center></div>');
	$.ajax({
		url: 'get_sub_categories.php',
		contentType: 'text/html; charset=UTF-8',
		type: 'GET',
		data: 'cat_id=' + cat_id + '&detailV='+ detailview,
		success: function(data)	{
			$('#loading').fadeOut(200);
			$(appe).append('<div id="product_box" class="goAway">'+data+'</div>');
			$('#loading').remove();
		}
	});
	return false;
}

function open_product_list(cat_id, main_id, detailV) {
	var main_id = (main_id == null) ? '' : main_id;
	var detailV = (detailV == null) ? false : detailV;
	
	if(!detailV){
		var appe = '#content';
	}
	else{
		var appe = '#detailNav';
		$('.pInfoPanorama').remove();	
		$('#content_info').append('<div id="productsFromCat"></div>');
	}
	if($('#loading')){
		$('#loading').remove();
	}
/*	$.each($('#'+cat_id),function()
			{
				this.onclick = '';
			});*/

	setActiveSubCat(cat_id);
	$.each($('.placeholder'),function() {
				$(this).remove();
			});
	$(appe).append('<div style="width:800px" id="loading"><center><img src="img/loading.gif" /></center></div>');
	$.ajax({
		url: 'get_product_list2.php',
		contentType: 'text/html; charset=UTF-8',
		type: 'GET',
		data: 'cat_id=' + cat_id + '&artId='+ main_id + '&detailV=' + detailV,
		success: function(data)	{
			$('#loading').fadeOut(200);
			$('#productsFromCat').append('<div id="placeholder" class="placeholder">'+data+'</div>');
			$('#loading').remove();
/*			$.each($('#'+cat_id),function()
					{
						this.onclick = function(){open_product_list(this.id);};
					});*/
				}
	});
	return false;
}

function setActiveSubCat(activeSubCat){
	$.each($('.aCat'),function(){
				if($(this).attr('id') == activeSubCat)
					$(this).addClass('active');
				else
					$(this).removeClass('active');
	});
}


function pBoxItem_show(pID) {
	$('.pBoxItem').animate({ 'height' : '62px'}, 300, 'linear');
	$('.pBoxItem').removeClass('pBoxActive');
	$(pID).animate({ 'height' : '232'}, 400, 'linear');
	$(pID).addClass('pBoxActive');
}


function load_slide(){
    var container = $('div.sliderGallery');
    var ul = $('.imageLibrary', container);
		var item = $('li', ul);
		ul.css("width", item.lenght * parseInt(item.css("width")));
    var itemsWidth = ul.innerWidth() - container.innerWidth() + 50;
    if(itemsWidth < 0){
			ul.css('left',(container.outerWidth() - ul.outerWidth())/2);
			productMaxWidth = 0;
			productMinWidth = 0;
			
			$('.slider', container).slider({ 
				min:productMinWidth, 
				max:productMaxWidth 
			});
			
			$('.slider').css('display', 'none');
			$('.slider', container).slider('destroy');
    }
    else{
			productMinWidth = 0;
			productMaxWidth = itemsWidth;
			$('.slider', container).slider({ 
				min:0, 
				max:productMaxWidth,
				handle:'.handle',
				stop: function(event,ui){
					ul.animate({'left' : ui.value * -1}, 500, 'linear');
				},
				slide: function (event, ui) {
					ul.css('left',  ui.value * -1 + "px");
				}
			});
    }

};

function navtrail(catID,subcatID) {
	$('#content_info').remove();
	$('#container_sl').remove();
	$('#product_box').remove();
	$('#content').append('<div class="grid_12" id="loading"><center><img src="img/loading.gif" /></center></div>');
	$.ajax({
		url: 'get_gategories.php',
		type: 'GET',
		data: 'cat_id=0&cat_type1=img',
		success: function(data){
			$('#loading').fadeOut(200);
			$('#loading').remove();
			$('#content').append('<div id="content_info"></div>');
			$('#content_info').append(data);
		}
	});
	if(open_cat(subcatID,'check') == false){
				open_sub_cat(subcatID);
			}
			
}



function oBlubb(blubbx){
 if(document.getElementById(blubbx).style.display=='block'){
  document.getElementById(blubbx).style.display='none';
 }else{
  document.getElementById(blubbx).style.display='block';
 }
}

function new_map2(map_name,maploc) {
  document.getElementById('mapx').src = maploc + 'img/' + map_name + '.png';	
}
function map_reset(maploc) {
  document.getElementById('mapx').src = maploc + 'img/europe_map.png';	
}

function go_country(map_locx,layer_name) {
  new_map2(layer_name,map_locx);
  
  for(i=1;i<25;i++){
  document.getElementById('layer_'+i).style.display = 'none';	
  }
  document.getElementById('layer_'+layer_name).style.display="block";	
}


function new_map(map_name) {
  document.getElementById('world_map').src = map_name + '.png';	
}

function go_map(map_name,layer_name) {
  new_map(map_name);
  document.getElementById('mleu').style.display = 'none';	
  document.getElementById('mlas').style.display = 'none';	
  document.getElementById('mlna').style.display = 'none';	
  document.getElementById('mlsa').style.display = 'none';	
  document.getElementById('mlau').style.display = 'none';	
  document.getElementById('mlaf').style.display = 'none';	
  document.getElementById(layer_name).style.display="block";	
}




function PressItem_show(pID) {
	$('.press_item').animate({ 'height' : '85px'}, 100, 'linear');
	$('.press_item').removeClass('pressItemActive');
	$(pID).animate({ 'height' : '232'}, 100, 'linear');
	$(pID).addClass('pressItemActive');
}
function PressItem_Close(pID) {
	$('.press_item').animate({ 'height' : '85px'}, 100, 'linear');
	$('.press_item').removeClass('pressItemActive');
}

function add_intro(lang){
	$('#content_info').append('<img src="/images/intro_images/intro_bilder_'+lang+'.gif" alt="Artwizz Intro" width="800" id="home_intro" />');
}

function show_youtube_vid(){

	var what = $('#vidSelect').val();
if(what)	
 var vidObj = '<object width="560" height="340"><param value="'+what+'" name="movie" /><param value="true" name="allowFullScreen" /><param value="always" name="allowscriptaccess" /><embed width="560" height="340" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" src="'+what+'"></embed></object>';
 	$('#youtubeVid').html(vidObj).fadeIn(400);
}
