/***************************************************************************
 * fone-shop js (new)
 ***************************************************************************/

/***************************************************************************
 * jQuery no Conflict Mode
 ***************************************************************************/
jQuery.noConflict();


/***************************************************************************
 * Allgemeines
 ***************************************************************************/
var tabopen;
var tabopen_id;
var refresher = '<div class="indicator" style="margin-top: 4px; text-align: center;"><img src="/img/indicator.gif" alt="indicator" /><span style="display: none">Bitte warten. Daten werden übertragen.</span></div>';

// prüfe ob schon eine anbieterauswahl stattgefunden hat. wenn nicht setzte ein standart (vodafone d2)
if (typeof(aid_cache) == 'undefined') {
	var aid_cache = '6';
}

var artikel_id_cache = '';


/***************************************************************************
 * Event Handler
 ***************************************************************************/
 
// Artikel Details TabNavigation
jQuery(document).ready(function() {
	jQuery('#tabnav a').click(function(){
		var tab = jQuery(this).text();
		var tab_go = tab_switch(tab);
		open_tab(tab_go);
		return false;
	});
});


// Tarifoptionen beschreibungstooltip. prüfe vorher ob cluetip geladen wurde
if (typeof(jQuery(document).cluetip) == 'function') {
	jQuery(document).ready(function() {
		jQuery('img.options_info').cluetip({
			cluetipClass: 'foneshop',
			dropShadow: true, 
	    	dropShadowSteps: 6,	
	    	width: 280,
			attribute: 'rel',
			activation: 'hover',
			cluezIndex: 105,
			arrows:true,
			positionBy: 'mouse',
		  	sticky: false,
		  	ajaxCache: true,
	 		fx: {             
	            open: 'show',
	            openSpeed:  ''
	    	}  	
	  	});
	});
}

// Varianten Select
jQuery(document).ready(function() {
	jQuery('#artikel_varianten').change(function() {
		var variante = jQuery(this).val();
		jQuery('#artikelnummer').val(variante);
		jQuery('#status_view').html(refresher);
		show_varianten_para();
	});
});

// Artikel Details Tarifanbieternavigation
jQuery(document).ready(function() {
	artikel_id_cache = jQuery('#artikel_varianten').attr('rel');
	jQuery('#tnav li.nav').click(function(){
		var aid = jQuery(this).attr('rel');
		nav_control(aid);
		return false;
	});
});



/***************************************************************************
 * Functions
 ***************************************************************************/
 
// Artikel Details Tarifanbieternavigation
function tab_switch(tab) {
	var tab_go = '';
	switch (tab) {
		case "Allgemein":
			tab_go = 'allgemein';
			break;
		case "Display":
			tab_go = 'displaysound';
			break;
		case "Komfort":
			tab_go = 'komfort';
			break;
		case "Kamera":
			tab_go = 'messagefun';
			break;
		case "Connect":
			tab_go = 'connectivity';
			break;
		default: tab_go = 'allgemein';
	}
	return tab_go;
}

// Artikel Details Tarifanbieternavigation
function nav_control(aid) {
	var aid_a = new Array('3','4','5','6');
	var klasse_a = new Array('esel','osel','tsel','vsel');
	var anbieter_a = new Array('eplus','o2','tmobile','vodafone');


	for (var i = 0; i < aid_a.length; ++i) {
		if (aid_a[i] == aid) {
			jQuery("#nc_"+aid_a[i]).show();
			jQuery("#tnav").attr('rel', aid_a[i]);
			jQuery("#"+anbieter_a[i]).addClass(klasse_a[i]);
			jQuery("#"+anbieter_a[i]+" a").html('<img width="135" height="29" title="'+anbieter_a[i]+'" alt="'+anbieter_a[i]+'" src="http://iimpic.de/fs/nav/provider_'+anbieter_a[i]+'_hover.gif"/>');
			aid_cache = aid_a[i];
		}
		else {
			jQuery("#nc_"+aid_a[i]).hide();
			jQuery("#"+anbieter_a[i]).removeClass(klasse_a[i]);
			jQuery("#"+anbieter_a[i]+" a").html('<img width="135" height="29" title="'+anbieter_a[i]+'" alt="'+anbieter_a[i]+'" src="http://iimpic.de/fs/nav/provider_'+anbieter_a[i]+'.gif"/>');
		}
	}
}

// Fetch Tarifliste for changes (Refresh)
function view_tariflist(artnummer) {
	jQuery('#tarif_box').html('');
	jQuery('#preloader_tariflist').show();
	jQuery.ajax({
  		type: "POST",
  		url: "/artikel_details.php",
  		data: "artikel_id="+artikel_id_cache+"&a_id="+aid_cache+"&artnummer="+artnummer+"&ajax=1",
  		success: function(msg) {
   			if (msg == 'fehler') {
    			jQuery('#tarifauswahl').html("Die Tarifliste konnte nicht geladen werden.");
   			}
   			else {
				jQuery('#tnav li.nav').livequery('click', function() {
					var aid = jQuery(this).attr('rel');
					nav_control(aid);
					return false;
				});
    			jQuery('#tarifauswahl').html(msg);
    			jQuery('#preloader_tariflist').hide();
   			}
     	}
	 });
}

// Tabnavigation -> Open Tab
function open_tab(t) {

	// Tabopen von php gesetzt auslesen
	if (jQuery('#tabnav .tabopen')) {
		var given_cat = jQuery('#tabnav .tabopen a').text();
		var given_cat_go = tab_switch(given_cat);
		tabopen = jQuery('#'+given_cat_go);
		tabopen_id = given_cat_go;
	}

	//tab einlesen
	if (typeof(t)=="string") tab = jQuery('#'+t);
	else tab = t;

	//altes tab die class entziehen und dem neuen zuweisen // übergabe der styles
	if (tabopen) {
		tabopen.removeClass('tabopen');
		jQuery('ul li.'+tabopen_id).removeClass('tabopen');
	}
	tab.addClass('tabopen');
	jQuery('ul li.'+t).addClass('tabopen');

	// Content zu Tab ein/ausblenden
	if (jQuery('#'+tabopen_id+' .tabkernel')) jQuery('#'+tabopen_id+' .tabkernel').css('display','none');
	jQuery('#'+t+' .tabkernel').css('display', 'block');
	jQuery('#'+t+' .tabkernel').css('z-index', 60);

	tabopen = tab;
	tabopen_id = t;

	return false;

}


// Bei Auswahl einer Artikelvariante werden erfoderliche Details entsprechend aktualisiert
function show_varianten_para(bool) {

	if(bool==null) bool = false;

	/* Prüfung ob ein Bild zu der ausgewählten Artikelvariante vorhanden ist, wenn ja, wähle das 1. dieser Var. aus */
	var artnr = jQuery('#artikel_varianten').val();
	var artikel_id = jQuery('#artikel_varianten').attr('rel');

	/* Wenn vorhanden, wird auch der Preis aktualisiert, entsprechend der ausgewählten Artikelvariante */
	//var new_price = jQuery('#preis_'+artnr).val();
	jQuery.ajax({
  		type: "POST",
  		url: "/lib/ajax/artikel_variante_preis.php",
  		data: "artikel_id="+artikel_id+"&varianten_id="+artnr,
  		success: function(msg) {
   			if (msg != 'error') {
				jQuery('#eboxpreis span').text(msg+' '+unescape("%u20AC")+' *');
   			}
     	}
	 });
	 
	 jQuery.ajax({
  		type: "POST",
  		url: "/lib/ajax/artikel_variante_preis_min.php",
  		data: "artikel_id="+artikel_id+"&varianten_id="+artnr,
  		success: function(msg) {
   			if (msg != 'error') {
				jQuery('#eboxpreis_ohne span').text(msg);
   			}
     	}
	 });



	if (artnr && artnr > 0) {

		/* Wenn Variante im Warenkorb vorhanden, wird der Bestellbutton getauscht */
		check_warenkorb_variante();

		// Wenn Varianten auf der Merkliste
		check_merkliste_variante();

		if(!bool) {
			var first_var_pic = jQuery('#'+artnr).val();
			if (first_var_pic && first_var_pic > 0) {
				open_galtab(first_var_pic); // without false working right?
			}
		}
	}
	view_tariflist(artnr);
}



/***************************************************************************
 * Buttons Hover Effect
 ***************************************************************************/

// Datenblatt Button
jQuery(document).ready(function() {
	jQuery('#artikeldatenblatt a img').mouseover(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/datenblatt_btn_hover.gif');
	});
	jQuery('#artikeldatenblatt a img').mouseout(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/datenblatt_btn.gif');
	});
});

// Warenkorb Button
jQuery(document).ready(function() {
	jQuery('#gobuy a img').mouseover(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/warenkorb_btn_hover.gif');
	});
	jQuery('#gobuy a img').mouseout(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/warenkorb_btn.gif');
	});
});

// Bestellen Button
jQuery(document).ready(function() {
	jQuery('#buy a img').mouseover(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/bestellen_btn_hover.gif');
	});
	jQuery('#buy a img').mouseout(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/bestellen_btn.gif');
	});
});

// Bestellbutton Tarife
jQuery(document).ready(function() {
	jQuery('#tarif_box a.btn img').mouseover(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/bestellen_btn_hover.gif');
	});
	jQuery('#tarif_box a.btn img').mouseout(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/bestellen_btn.gif');
	});
});

// Tarifdetails Button
jQuery(document).ready(function() {
	jQuery('#tarif_info p.link img').mouseover(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/tarifdetails_btn_hover.gif');
	});
	jQuery('#tarif_info p.link img').mouseout(function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/btn/tarifdetails_btn.gif');
	});
});

// Tarif Navigation Button
jQuery(document).ready(function() {
	
	jQuery('#tnav li#vodafone img').livequery('mouseover', function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_vodafone_hover.gif');
	});
	jQuery('#tnav li#vodafone img').livequery('mouseout', function(){
		if (jQuery('#tnav').attr('rel') != 6) {
			jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_vodafone.gif');
		}
	});
	jQuery('#tnav li#tmobile img').livequery('mouseover', function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_tmobile_hover.gif');
	});
	jQuery('#tnav li#tmobile img').livequery('mouseout', function(){
		if (jQuery('#tnav').attr('rel') != 5) {
			jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_tmobile.gif');
		}
	});
	jQuery('#tnav li#eplus img').livequery('mouseover', function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_eplus_hover.gif');
	});
	jQuery('#tnav li#eplus img').livequery('mouseout', function(){
		if (jQuery('#tnav').attr('rel') != 3) {
			jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_eplus.gif');
		}
	});
	jQuery('#tnav li#o2 img').livequery('mouseover', function(){
		jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_o2_hover.gif');
	});
	jQuery('#tnav li#o2 img').livequery('mouseout', function(){
		if (jQuery('#tnav').attr('rel') != 4) {
			jQuery(this).attr('src', 'http://iimpic.de/fs/nav/provider_o2.gif');
		}
	});
});


// Preload Images
//bestell_btn_hover = new Image(143,29); 
//bestell_btn_hover.src="http://iimpic.de/fs/btn/bestellen_btn_hover.gif"; 
