/*

	Customized Javascript Code for Native Union
	Author: 		Leung Kwan Pang
	Version:		1.0 (November 22, 2009)
	
*/

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

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

	if ($("body").hasClass('page_type_product')) {
		$('#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'
		});
	}

	$('select.quantity_select').bind('change', function() {
		if ( $(this).val() == 'more') {
			$(this).hide();
			$(this).next('input').show().focus();
		}
	});
	
	$('.page_title_products .photos ul li').hover(function() {
		var box = '.box_'+this.className.substring(6);
		$(this).find('span').css('border','1px solid #666');
		$('.text_box div').hide();
		$(box).show();
	}, function() {
		$(this).find('span').css('border','1px solid #FFF')
//		$('.text_box div').hide();
//		$('.text_box .box_products').show();
	});
});

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

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

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

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

function addToShoppingCart() {
	viewShoppingCart();
}

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

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

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

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

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

function showSecurityCode() {
	$('#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 = $(obj).position().top + $(obj).height() - $('.'+box).height() + 140;
		var filename = '../_imgs/photo_'+model+'_box.jpg';
		$('.popup_whatyouget img').attr('src',filename);
//		alert(filename);
	} else {
		var top = $(obj).position().top + $(obj).height() - $('.'+box).height() + 60;
	}
	var left = $(obj).position().left - $('.'+box).width();
	$('.'+box)
	.css('left', left)
	.css('top', top);
	$('.'+box).toggle('slow');
}

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

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

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

function chooseColor(code, color, obj) { /* (color, obj) {
	default_color = color;
	$('.box_addtocart .color').css('background','none');
	$(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) {
	$(box).siblings('a').addClass('subnav').removeClass('strong');
	$(box).removeClass('subnav').addClass('strong');
	var main = $(box).parent().parent().find('.text_main');
	$(main).find('p:visible').slideUp('slow', function() {
		$(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();
$(document).ready(function() {
	$.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();
		}
	});
	$.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() {
	$('select.phoneBrand').html("<option> - Brand - </option>");
	for ( var k = 0 ; k < brand_list.length ; k++ ) {
		$('select.phoneBrand').append("<option value=\'"+brand_list[k]+"\'>"+brand_list[k]+"</option>");
	}
}

function getPhoneModel(obj) {
	var brand = obj.value;
	var theList = $(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 = $(obj).parent().children('.adapter_text')
	var result = $(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++ ) {
		$('#select_country_list').append("<option value='"+city_list[i].country+"'>"+city_list[i].country+"</option>");
	}
}

function showRetailerCities(country) {
	var j;
	$('#select_city_list').empty();
	$('#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++ ) {
		$('#select_city_list').append("<option value='"+city_list[j].cities[k]+"'>"+city_list[j].cities[k]+"</option>");
	}
	$('#select_city_list').show();
}

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

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

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

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

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

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

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

// For About Us and Health Issues
$(document).ready(function() {
	$('.page_title_about_us .news_list > li > a').click(function() {
		var obj = $(this);
		if ($(this).parent().children().length > 1) {
			obj.removeClass('strong').parent().find('div').slideUp('slow', function(){$(this).remove()});
		} else {
			$.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) {
	$(obj).parent().parent().prev().removeClass('strong')
	$(obj).parent().parent().slideUp('slow', function() {$(this).remove()});
}

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

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