//jQuery.noConflict();
jQuery(document).ready(function() {

jQuery.extend({
  newElement : function(elementName) {
    return jQuery(document.createElement(elementName));
  }
});



jQuery(function($) {
   

  // Teaser Image Fade //
  if($('#hometeaser').length) {
  $(".timage").css("visibility","visible");
  if($("#hometeaser .timage img").length > 1) {
  $('#hometeaser .timage').innerfade({
				animationtype: 'fade',
				speed: 2250,
				timeout: 6000,
				type: 'random',
				containerheight: '226px'
			});
 }
 }
 
 
  // Headerpic Fading //
    if($('.emotionpic').length) {
    $(".emotionpic .epicfade").css("visibility","visible");
   if($(".emotionpic .epicfade img").length > 1) {
  $('.emotionpic .epicfade').innerfade({
				animationtype: 'fade',
				speed: 2250,
				timeout: 6000,
				type: 'random',
				containerheight: '226px'
			});
 }
 }
 
 
 // Init Shadowbox
 if(typeof Shadowbox !== 'undefined') {
 Shadowbox.init();
 }
 


 // Jquery Reflect aktivieren
 
 //if($("#content .csc-textpic-image img").length > 0) {
 //$('#content div.csc-textpic img').reflect({height: 0.5, opacity: 0.2} );
 //}

        
     
 /* Show/hide element functions */
function show(id)
{
	var element = document.getElementById(id);
	element.style.visibility = 'visible';
}
function hide(id)
{
	var element = document.getElementById(id);
	element.style.visibility = 'hidden';
	element.style.display = 'none';
}

   
// Sidescroller aktivieren falls erforderlich

 if($("div.containerScroller .sidescrollerBoxes div")) {
      
      // Initialisieren der Variablen für Container Inhalt
      var breite = 0;
	    var aktBreite = 0;
	    var aktPosition = 0;
	    



      $("div.containerScroller").each(function(){
      // 
      if($("div#" + $(this).attr("id") + ".containerScroller .sidescrollerBoxes div > a").size() > 4) {

    // Breite jedes Bildes ermitteln  
    $("div#" + $(this).attr("id") + ".containerScroller .sidescrollerBoxes div a img").each(function (i) 
	  {
		breite = $(this).attr("width");
		aktBreite += parseInt(breite) + 17; 
	 }); 


	    
       initSidescroller(400, aktBreite, 2, $(this).attr("id"));
       
      }else{
         // Falls weniger als 5 Elemente im Container sind
         // Navigationspfeile ausblenden
      		$('div#'+ $(this).attr("id")  + '.containerScroller .sidescrollerLeft').hide ();
		      $('div#'+ $(this).attr("id") + '.containerScroller .sidescrollerRight').hide ();
      }
      
      
      
      	     // Aktuelle Position des aktiven Elements ermitteln
    $('div#'+ $(this).attr("id")  + ".containerScroller .containerScrollerbox a img").each(function (i) 
	  {
	    
	    breite = $(this).attr("width");
    
      aktPosition += parseInt(breite) + 17;
	   
	    
	   	if($(this).parent().parent().is('.slink.active')) {
      return false;
    }
    

	    }); 
	    
	    //alert(aktPosition);
      
      
      }
      );
      
      
      $('div.containerScroller .containerScrollerbox a').bind('click', function() {
        actcontainer_id = $(this).parent().parent().parent().parent().attr("id");
        //$('div#'+ actcontainer_id  +' .sidescrollerBoxes').css ('left', '0px');
        imgid = $(this).find('img').attr("id");
        $('div.containerScroller .containerScrollerbox a').attr("class","slink");
        $(this).attr("class", "slink active");
        cObj = imgid.split('-')[2];
        uid = imgid.split('-')[1];
        loadContent(uid,cObj);
     
    });
    
    
    	    
	     // Init History
 if(typeof $.history  !== 'undefined') {
 

        $.history.init(function(url) {
                if(url){
                  act_link = 'slink-' +url.split('-')[1];
                  if(act_link) {
                  $('div.containerScroller .boxes .containerScrollerbox a#' + act_link).trigger('click');
                  }
                }
            
            });


 }


       
    // Falls kein Produkt gewählt wurde das erste element aktivieren //
    if (!$('div.containerScroller .containerScrollerbox a').is('.slink.active')) {
        $('div.containerScroller .boxes .containerScrollerbox:first a').trigger('click');
    }
    	 


   


 }
 




function loadContent(uid,cObj){

//Ajaxabfrage stellen
		$.ajax({
			url: 'index.php',		// Achtung mit RealURL!
			type: "GET",			// Daten per GET verschicken
			data: {
				type: "5000",
			 uid: uid,
				c: cObj
		  },
			dataType: "json",		// das gibts zurück
			// Es hat funktioniert?
			success: function(response) {
				// Code der ausgeführt wenns geklappt hat
				//var obj = $.parseJSON(response);

        var content = ''; 
        
        
				if(response) {
           
           content = '<div id="productcwrap">';
				   content += '<h2>' + response.title.title + '</h2>';


				   
				   	//	Bilder zusammenbauen	
	        var origImages = response.images;
	        var thumbnails = response.thumbnails;
	        
	        if(thumbnails.length >= 1) {
           content += '<div class="two-col">';
          }
          
          
         content += '<div class="pdesc">' + response.description + '</div>';
         
         if(thumbnails.length >= 1) {
          content +='<div class="pimg">';
         }
         
         if(thumbnails.length == 2) {
          content +='<div class="oloimg">';
         }
         	        
	       for(var i=1; i < thumbnails.length; i++){
		        var tmpImg = new Image();
		         tmpImg = thumbnails[i];
		            
		           content += '<a href="' + origImages[i] + '" rel="shadowbox[pimg]" onclick="Shadowbox.open({player:\'img\', title:\'\', content:\''+origImages[i] +'\'}); return false" >' + '<img src="' +  thumbnails[i] + '" /></a>';
	          	//content += new Element('a',{'class' : 'guitarshow-thumbnails','rel' : 'lightbox[lb]',href : origImages[i]}).update(tmpImg);

        	}
        	
        	
        	if(thumbnails.length == 2) {
        	  content +='</div>';
        	}
        	
        	if(thumbnails.length >= 1) {
          content +='</div></div>';
         }
        	
        	 content += '</div>';

			     $('#responseText').html(content);
				}


			},

			// Schade, es war ein Fehler
			error: function(error) {
				alert("Sorry, hat nicht funktioniert");
			}
		});

	
}   


function updateRequest (orgRequest){
	var res = orgRequest.responseText.evalJSON(true);
	var twoCol = new Element('div',{'class' : 'two-col'});
	var leftCol = new Element('div',{'class' : 'left'});
	var rightCol = new Element('div',{'class' : 'right'});

//	Überschrift
	var h1 = new Element('h1',{'class' : 'guitarshow-replaced'}).update(res.title.title);
	h1.setStyle({
		width: res.title.width+'px',
		backgroundImage: 'url('+res.title.path+')'
	});
	
	leftCol.update(h1);
	leftCol.innerHTML += res.description;

	//	Bilder zusammenbauen	
	var origImages = $A(res.images);
	var thumbnails = $A(res.thumbnails);
	for(var i=1; i < thumbnails.length; i++){
		var tmpImg = new Image();
		tmpImg.src = thumbnails[i];
		
		var a = new Element('a',{'class' : 'guitarshow-thumbnails','rel' : 'lightbox[lb]',href : origImages[i]}).update(tmpImg);
		rightCol.appendChild(a);
	}

	//	Alles zusammenbauen
	twoCol.appendChild(leftCol);
	twoCol.appendChild(rightCol);
	$('second-content').update(twoCol);
	myLightbox.updateImageList();
}   
 
});
});




