/*++++++++++++++++++++++++++++++++++

scripts.bb.mobile.productgallery.js

++++++++++++++++++++++++++++++++++*/              
                                                                                    
var colour_ajax_loader = '/images/design/broadband/mobile/ajax_gallery_loader_v2.gif';
var plan_name = "";
$(
	function () {
		if ($('.colour-chooser').length) {
			observeColourChooser();
		}
	    
		plan_name = $('.package-confirmation strong');
		GATextended('shop','broadband','3g',plan_name,'unknown','device_gallery','choose_device','browse_5-1','router','evaluate_alternatives');     
		
		GAobserver();
		
	}
) 


function observeColourChooser(){
	preload_img = new Image();
	preload_img.src = colour_ajax_loader;
	$('.colour-chooser a').bind("click", function(){
		var parent_div_id = $(this).parent('li').parent('ul').parent('.colour-chooser').parent('.product-image').parent('.product').attr('id');
		
		var u = $(this).attr('rel');
		$('#' + parent_div_id + ' .colour-chooser a').removeClass('active');
		$('#' + parent_div_id + ' .product-image>img').attr('src', colour_ajax_loader).attr('alt', "");
		$(this).addClass('active');
	  	
		var device_name = $('#' + parent_div_id + ' .product-description h2').text();
		                  
		$.ajax({
			url: u,
			cache: false,
			success: function(data) {
			   	xmlarray = xml2Array(data);
				var image_src = xmlarray['imageSrc'];
				var image_alt = xmlarray['imageAlt'];
				var price_euro = xmlarray['priceEuro'];
				var price_cent = xmlarray['priceCent'];
				if (xmlarray['buyUrl']) {
					var buy_url = xmlarray['buyUrl'].replace(/&amp;/gi, '&'); 
				} else {
					var buy_url = ""; 
				}
				var colour_text = xmlarray['colourText'];
				var more_details_url = xmlarray['moreDetailsUrl'].replace(/&amp;/gi, '&');
				
				if (xmlarray['RRP'].substring(1, (xmlarray['RRP'].length)) == '0.00') {
				    var rrp = ""; 
				} else if (jQuery.trim(xmlarray['RRP'])) {
					var rrp = xmlarray['RRP'];
				} else {
					var rrp = "";
				}
				                                                                                                        
				 
				if (xmlarray['onlineSaving'].substring(1, (xmlarray['onlineSaving'].length)) == '0.00') {
				    var online_saving = "";
				} else if (jQuery.trim(xmlarray['onlineSaving'])) {
					var online_saving = xmlarray['onlineSaving'];
				} else {
					var online_saving = "";
				}
				
				if (xmlarray['description']) {
					$('#' + parent_div_id + ' .info h2, #' + parent_div_id + ' .product-description h2').html(xmlarray['description']);
				}
				                                              
				if ($('#' + parent_div_id + ' .product-image>img').length) {
					$('#' + parent_div_id + ' .product-image>img').attr('src', image_src).attr('alt', image_alt); 
				} else if ($('#' + parent_div_id + ' .product-image>a>img').length) {
					$('#' + parent_div_id + ' .product-image>a').attr('href', more_details_url);
					$('#' + parent_div_id + ' .product-image>a>img').attr('src', image_src).attr('alt', image_alt); 
				}
				 
				$('#' + parent_div_id + ' .pricing .euro').html(price_euro);
				$('#' + parent_div_id + ' .pricing .cent').html(price_cent);
				$('#' + parent_div_id + ' .colour-text strong').html(colour_text);
				
				if (buy_url) {
					if ($('#' + parent_div_id + ' .product-pricing a').length) {                           
						$('#' + parent_div_id + ' .product-pricing a').show().attr('href', buy_url);
					} else {
						$('#' + parent_div_id + ' .product-pricing').append('<a href="' + buy_url + '"><img src="/images/buttons/buy_now.png" alt="" class="rollover" /></a>');
					}
					$('#' + parent_div_id + ' .product-pricing .out-of-stock').hide();
				} else {              
					if ($('#' + parent_div_id + ' .product-pricing .out-of-stock').length) {
						$('#' + parent_div_id + ' .product-pricing .out-of-stock').show();
					} else {
						 $('#' + parent_div_id + ' .product-pricing').append('<img src="/images/buttons/sold_out.png" alt="" class="out-of-stock rollover" />');
					}
					$('#' + parent_div_id + ' .product-pricing a').hide().attr('href', "");
				}
				          
				$('#' + parent_div_id + ' .product-features a').attr('href', more_details_url);
				
				if (rrp) {
					$('#' + parent_div_id + ' .retail-price').removeClass('hidden');
					$('#' + parent_div_id + ' .retail-price strong').html(rrp);     
				} else {
					$('#' + parent_div_id + ' .retail-price').addClass('hidden');
					$('#' + parent_div_id + ' .retail-price strong').html('');
				}
				
				if (online_saving) {
					$('#' + parent_div_id + ' .online-saving').removeClass('hidden');
					$('#' + parent_div_id + ' .online-saving strong').html('saving ' + online_saving + ' online');     
				} else {
					$('#' + parent_div_id + ' .online-saving').addClass('hidden'); 
					$('#' + parent_div_id + ' .online-saving strong').html('');
				}                                                                       
			
                GATextended('shop','broadband','3g',plan_name,device_name,'device_gallery','select_colour_' + colour_text ,'browse_5-2','router','evaluate_alternatives');	    
			
			}
		});
		return false;
	});
}

function GAobserver(){
	$.each($('.product-listing .button-row'), function(){
		var device_name = $(this).parent('.info').children('h2');
		$(this).children('.buy-now').bind('click', function(){
			GATextended('shop','broadband','3g',plan_name,device_name,'device_gallery','add_to_cart','browse_5-4','closer','purchase_decision');
		});
		$(this).children('.modem-details').bind('click', function(){
			GATextended('shop','broadband','3g',plan_name,device_name,'device_gallery','view_device_details','browse_5-3','explainer','evaluate_alternatives');
		});
	});
	
}
