/*

	Customized Javascript Code for Native Union
	Author: 		Freelance HK
	Version:		1.0 (Sep. 22, 2010)

*/
if (top.location != self.location) top.location=self.location;

var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/; // There's another IE6 detection in jQuery(document).ready
var action = location.search.substring(8);
var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var default_color = '';

jQuery(document).ready(function() {
	
	IE6 = (typeof document.body.style.maxHeight == "undefined");
	
	// front page gallery
	if(jQuery("#gallery").length == 1){
		slideShow();
	}
	
	
	if (jQuery("body").hasClass('page_type_gallery')) {
		jQuery('#photos').galleryView({
			panel_width: 1000,
			panel_height: 510-75,
			frame_width: 75,
			frame_height: 75,
			border: 'none'
		});
	} else {
		fixVerticalCenter();
		jQuery(window).bind('resize', function() {
			fixVerticalCenter();
		});
		if (action == "viewcart") viewShoppingCart();
	};




	if (jQuery("body").hasClass('page_type_product')) {
		jQuery('#slide_photos').galleryView({
			panel_width: 520,
			panel_height: 477,
			frame_width: 30,
			frame_height: 30,
			border: 'none',
			nav_theme: 'grey',
//			background_color: '#ffc000'
			background_color: '#FFFFFF'
		});
	}

	jQuery('select.quantity_select').bind('change', function() {
		if ( jQuery(this).val() == 'more') {
			jQuery(this).hide();
			jQuery(this).next('input').show().focus();
		}
	});
	
	jQuery('.page_title_products .photos ul li').hover(function() {
		var box = '.box_'+this.className.substring(6);
		jQuery(this).find('span').css('border','1px solid #666');
		jQuery('.text_box div').hide();
		jQuery(box).show();
	}, function() {
		jQuery(this).find('span').css('border','1px solid #FFF')
//		jQuery('.text_box div').hide();
//		jQuery('.text_box .box_products').show();
	});
	
	var changeTimer;
	var pickerTimer;
	jQuery('#lang-picker').mouseover(function() {
		clearTimeout(pickerTimer);
		jQuery('#lang-picker').addClass('hover');
	}).mouseout(function() {
		clearTimeout(pickerTimer);
		pickerTimer = setTimeout(function() { jQuery('#lang-picker').removeClass('hover'); }, 500);
	});
	
	jQuery('#footer div.lang a.change').click(function() { return false; }).hover(function() {
		clearTimeout(changeTimer);
		jQuery('#lang-picker').addClass('on');
	}, function() {
		clearTimeout(changeTimer);
		changeTimer = setTimeout(function() { jQuery('#lang-picker').removeClass('on'); }, 500);
	});
});

jQuery(window).load(function() {
	//jQuery("#shoppingcart .warning").liquidCanvas("[shadow{width:3; color:\'#333\'; shift:2} fill{color:#FFF}] => roundedRect{radius:8}");
});

function fixVerticalCenter() {
	// if (jQuery(window).height() < jQuery('#page').height() ) {
	// Webkit browsers sometimes miscalculate the height of the main div when loading the page;
	if (jQuery(window).height() < 673 ) {
		jQuery('#page').css('margin-top',0).css('top',0);
	} else {
		jQuery('#page').css('margin-top','-336px').css('top','50%');
	}
	if (jQuery(window).width() < jQuery('#page').width() ) {
		jQuery('#page').css('margin-left',0).css('left',0);
	} else {
		jQuery('#page').css('margin-left','-500px').css('left','50%');
	}
}

function viewVideo(product) {
	GB_show("../_incs/video.php?action="+product,520,1000);
}

function viewShoppingCart() {
  if(IE6){
		jQuery('.text_box select').css('display', 'none');
  }
	jQuery.ajax({
		url: "cart.php",
		cache: false,
		success: function(html){
			jQuery('#shoppingcart').html(html);
			jQuery('#contentmask').css('opacity', '0');
			jQuery('#contentmask').css('display', 'block');
			jQuery('#contentmask').fadeTo("slow", 0.8, function(){
				jQuery('#shoppingcart .cartbanner').slideUp("fast", function(){
					jQuery('#shoppingcart .cartboard').slideDown("slow")
				});
			});
		}
	});
}

function addToShoppingCart() {
	viewShoppingCart();
}

function closeShoppingCart() {
	jQuery('#shoppingcart .cartboard').slideUp("slow", function(){
		jQuery('#shoppingcart .cartbanner').slideDown("slow", function(){
			jQuery('#contentmask').fadeTo("slow", 0, function(){
				jQuery('#contentmask').css('display', 'none');
				 if(IE6){
					jQuery('.text_box select').css('display', 'inline');
				}
			})
		});
	});
}

function checkAdapterWarning() {
	jQuery('#shoppingcart .adapter_warning').show();
	getBrandList()
}

function showAdapterChecker(box) {
	jQuery(box).parent().next('.box_addtocart').find('.adapter_checker').slideToggle("slow");
}

function focusTextField(obj) {
	if (jQuery(obj).hasClass('tips')) {
		jQuery(obj).removeClass('tips').val('');
	}
}

function blurEmailOffer(obj) {
	if (jQuery(obj).val() == '') {
		jQuery(obj).addClass('tips').val('Put email address here')
	}
}

function showSecurityCode() {
	jQuery('#securitycode').toggle('slow');
}

function getImageGallery(product,id,color) {
	if (!id) id=1;
	GB_show("gallery.php?action="+product+"&id="+id+"&color="+color,520,1000);
}

function popup(box, obj, model) {
	if ( box == 'popup_whatyouget' ) {
		var top = jQuery(obj).position().top + jQuery(obj).height() - jQuery('.'+box).height() + 140;
		var filename = '../_imgs/photo_'+model+'_box.jpg';
		jQuery('.popup_whatyouget img').attr('src',filename);
//		alert(filename);
	} else {
		var top = jQuery(obj).position().top + jQuery(obj).height() - jQuery('.'+box).height() + 60;
	}
	var left = jQuery(obj).position().left - jQuery('.'+box).width();
	jQuery('.'+box)
	.css('left', left)
	.css('top', top);
	jQuery('.'+box).toggle('slow');
}

function closePopup(box) {
	jQuery('.'+box).hide('slow');
}

function showChangeCountry() {
	jQuery('.changecountry').toggle('slow');
}

function showTellAFriend() {
	jQuery('.tellafriend').toggle('slow');
}

function chooseColor(code, color, obj) { /* (color, obj) {
	default_color = color;
	jQuery('.box_addtocart .color').css('background','none');
	jQuery(obj).css('background','transparent url(../_imgs/color_bg.png) center center no-repeat');
	*/
	var url = "product_redirect.php?code=" + code + "&color=" + color;
	default_color = color;
	location.replace (url);
}

function showText(sub, box) {
	jQuery(box).siblings('a').addClass('subnav').removeClass('strong');
	jQuery(box).removeClass('subnav').addClass('strong');
	var main = jQuery(box).parent().parent().find('.text_main');
	jQuery(main).find('p:visible').slideUp('slow', function() {
		jQuery(main).find('p.'+sub).slideDown('slow');
	});
}

// Where to buy functions
var city_list = new Array();
city_list.push({country:'China',cities:['Beijing']});
city_list.push({country:'Japan',cities:['Tokyo']});
city_list.push({country:'Hong Kong',cities:['Hong Kong']});
city_list.push({country:'United States',cities:['New York']});
city_list.push({country:'United Kingdom',cities:['London']});

// Phone List Functions 2010-01-30 ------------------------------------
var phone_list = new Array();
var adaptor_list = new Array();
var adaptorkit_list = new Array();
var brand_list = new Array();
/*jQuery(document).ready(function() {
	jQuery.ajax({
		url: "../_incs/PhoneList.csv",
		cache: false,
		success: function(html){
			phone_list = html.split('\r\n')
			for (var i=0; i<phone_list.length; i++) {
				adaptor_list.push(phone_list[i].split(','))
			}
			for (var j=0; j<adaptor_list.length; j++) {
				if(brand_list.toString().search(adaptor_list[j][0])==-1) {
					brand_list.push(adaptor_list[j][0])
				}
			}
			brand_list.sort();
			getBrandList();
		}
	});
	jQuery.ajax({
		url: "../_incs/AdaptorKitList.txt",
		cache: false,
		success: function(html){
			var list = html.split('\r\n')
			for (var i=0; i<list.length; i++) {
				adaptorkit_list.push(list[i].split(','))
			}
		}
	});
});*/

function getBrandList() {
	jQuery('select.phoneBrand').html("<option> - Brand - </option>");
	for ( var k = 0 ; k < brand_list.length ; k++ ) {
		jQuery('select.phoneBrand').append("<option value=\'"+brand_list[k]+"\'>"+brand_list[k]+"</option>");
	}
}

function getPhoneModel(obj) {
	var brand = obj.value;
	var theList = jQuery(obj).parent().children('select.phoneModel')
	theList.html("<option> - Model - </option>");
	for ( var i = 0 ; i < adaptor_list.length ; i++ ) {
		if ( adaptor_list[i][0] == brand) {
			theList.append("<option value=\'"+adaptor_list[i][2]+"\'>"+adaptor_list[i][1]+"</option>");
		}
	}
}

function checkIfAdapterNeeded(obj) {
	var adaptor_model = obj.value;
	var adaptorkits = new Array();
	for (var i=0; i<adaptorkit_list.length; i++) {
		for (var j=0; j<adaptorkit_list[i].length; j++) {
			if (adaptorkit_list[i][j] == adaptor_model) {
				adaptorkits.push(adaptorkit_list[i][0]);
			}
		}
	}
	var text = jQuery(obj).parent().children('.adapter_text')
	var result = jQuery(obj).parent().children('.result')
	if (adaptor_model == 'n/a') {
		text.html('<span>You don\'t need the adatper kit.</span>');
		result.html('');
	} else {
		text.html('You need an adapter kit.')
		result.html('<select id="adapter" class="adaptorkit">');
		result.children('select.adaptorkit').append('<option> - Choose - </option>');
		for (var i=0; i<adaptorkits.length; i++) {
			var adapter_value = "";
			if(adaptorkits[i]=="Adapter Pack A")
				adapter_value = "8";
			else if(adaptorkits[i]=="Adapter Pack B")
				adapter_value = "9";
			else if(adaptorkits[i]=="Adapter Pack C")
				adapter_value = "10";
			else if(adaptorkits[i]=="Adapter Pack Japan")
				adapter_value = "11";
			else
				adapter_value = "";
			result.children('select.adaptorkit').append('<option value=\''+adapter_value+'\'>'+adaptorkits[i]+'</option>');
		}
		result.append('<a href="#" class="button_black" onclick="add2Cart_adapter();">Add to Cart</a>');
	}
}

// ---------------------------------------------------------------------
function showRetailerCountries() {
	for ( var i = 0 ; i < city_list.length ; i++ ) {
		jQuery('#select_country_list').append("<option value='"+city_list[i].country+"'>"+city_list[i].country+"</option>");
	}
}

function showRetailerCities(country) {
	var j;
	jQuery('#select_city_list').empty();
	jQuery('#select_city_list').append("<option> Then select a city </option>");
	for ( var i = 0 ; i < city_list.length ; i++ ) {
		if (city_list[i].country == country) j=i;
	}
	for ( var k = 0 ; k < city_list[j].cities.length ; k++ ) {
		jQuery('#select_city_list').append("<option value='"+city_list[j].cities[k]+"'>"+city_list[j].cities[k]+"</option>");
	}
	jQuery('#select_city_list').show();
}

function showRetailers(city) {
	var str = city.replace(/\s/g,'_');
	jQuery('#select_retailer_list li').hide();
	jQuery('#select_retailer_list li.'+str).show();
	jQuery('#select_retailer_list').show();
}

function toggleMM01() {
	if (jQuery('.text_box_mm01').is(":hidden")) {
		jQuery('.text_box_mm01h').slideUp('slow');
		jQuery('.text_box_mm01').slideDown('slow');
	} else {
		jQuery('.text_box_mm01').slideUp('slow');
		jQuery('.text_box_mm01h').slideDown('slow');
	}
}

function GB_show(url, height, width) {
	try {
		GB_HEIGHT = height || 400;
		GB_WIDTH = width || 400;
		if (!GB_DONE) {
			jQuery("body #page").append("<div id='GB_window'><a href='#'class='close_button'>[ Close ]</a></div>");
			jQuery("#GB_window a").click(GB_hide);
			jQuery(window).resize(GB_position);
			jQuery('.close_button').textDropShadow();
			GB_DONE = true;
		}

		jQuery("#GB_frame").remove();
		jQuery("#GB_window").append("<iframe id='GB_frame' src='"+url+"' scrolling='no' border='0' frameborder='0'></iframe>");
		GB_position();
		
		jQuery("#GB_window").slideDown("slow");
	} catch(e) {
		alert( e );
	}
}

function GB_hide() {
	jQuery("#GB_window").slideUp("slow");
	jQuery('#GB_window').slideUp('slow', function() {
		killWindow();
	});
}

function killWindow() {
	jQuery("#GB_frame").remove();
	//jQuery('#GB_window iframe').empty();
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	jQuery("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px"});
  jQuery("#GB_frame").css("height",GB_HEIGHT +"px");
}

// For About Us and Health Issues
jQuery(document).ready(function() {
	jQuery('.page_title_about_us .news_list > li > a').click(function() {
		var obj = jQuery(this);
		if (jQuery(this).parent().children().length > 1) {
			obj.removeClass('strong').parent().find('div').slideUp('slow', function(){jQuery(this).remove()});
		} else {
			jQuery.ajax({
				url: "news.php?id="+obj.attr('id'),
				cache: false,
				success: function(html){
						obj.after('<div>'+html+'<p class="close small">[ <a href="#" onclick="removeIt(this)">Close</a> ] </p></div>');
						obj.addClass('strong').parent().find('div').slideDown('slow');
				}
			});
		}
	})
});

function removeIt(obj) {
	jQuery(obj).parent().parent().prev().removeClass('strong')
	jQuery(obj).parent().parent().slideUp('slow', function() {jQuery(this).remove()});
}

function pressFilter() {
	var c = jQuery('#filter_country').val();
	var m = jQuery('#filter_media').val();
	jQuery('.text_box_left .text_box li').hide();
	jQuery('.text_box_left .text_box li').filter(function(index) {
		if (c=='') c='.+'
		if (m=='') m='.+'
		var rg = new RegExp(c+'_'+m+'_','i');
		return jQuery(this).attr("id").match(rg)
	}).show();
}



function slideShow() {

	//Set the opacity of all images to 0
	jQuery('#gallery a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	jQuery('#gallery a:first').css({opacity: 1.0});
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = (jQuery('#gallery a.show')?  jQuery('#gallery a.show') : jQuery('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? jQuery('#gallery a:first') :current.next()) : jQuery('#gallery a:first'));	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 2000);

	//Hide the current image
	current.animate({opacity: 0.0}, 2000)
	.removeClass('show');
		
	
}






//jQuery Plugin: Drop Shadow Text
// call like this: jQuery(element).textDropShadow();
(function(jQuery) {
 jQuery.fn.textDropShadow = function(){
	 jQuery(this).html('<span class="jq-shadow">'+jQuery(this).html()+'</span><span class="jq">'+jQuery(this).html()+'</span>');
	 return jQuery(this);
 }
})(jQuery);
