// javascript initial
function initialize(googleLat, googleLng) {

           var map = new GMap2(document.getElementById("googleMap"));
           map.setCenter(new GLatLng(googleLat, googleLng), 14);
           map.addOverlay(new GMarker(new GLatLng(googleLat, googleLng)));
           var customUI = map.getDefaultUI();

           customUI.maptypes.normal = true;
           customUI.maptypes.satellite = true;
           customUI.maptypes.hybrid = true;
           customUI.maptypes.physical = true;
           customUI.zoom.scrollwheel = true;
           customUI.zoom.doubleclick = true; //default is is true.  This allows users to double-click on the map to automatically zoom.
           customUI.keyboard = false;
           customUI.controls.largemapcontrol3d = true; //default value is true for default larger maps.
           map.setUI(customUI);


 }

$(function(){
	//init mouse hover effect
	initBtn($(".login_btn"), void(0));
	initBtn($(".left_menu a.room"), void(0));
	initBtn($(".left_menu a.food"), void(0));
	initBtn($(".left_menu a.store"), void(0));
	initBtn($(".left_menu a.active"), void(0));
	initBtn($("a.more_btn"), void(0));
	initBtn($("a.viewmore_btn"), void(0));
	initBtn($(".submit-btn"));
	initBtn($(".form-submit-btn"), false, 'hoverBtn');
	
	$('.low_container .news_container .news_box .content  a.more_btn img').hover(
		function (){
			$(this).attr('src', $(this).attr('on'));
		}, function (){
			$(this).attr('src', $(this).attr('out'));
		}
	);
//	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
   		/*if (document.cookie.indexOf("iphone_redirect=false") == -1) {
			window.location = "http://m.espn.go.com/wireless/?iphone&i=COMR";
		}*/
		//alert("no flash");
/*		$("#imenu").removeClass(".hide");
	}else{
		$(".header").append("<div id='flashMenu'/>")
		vars = {};
		vars.setupXML = "/xml/menu.xml";
		var params = {};
		params.wmode = "transparent";
		var attributes = {};
		attributes.style = "display:block;"
		swfobject.embedSWF("/swf/menu.swf", "flashMenu", "100%", "100%", "10.0.0","expressInstall.swf", vars, params, attributes);
	}*/
	/*
	$(".slidebox_flash_bg").append("<div id='flashBG1'/>")
	vars = {};
	vars.ppsec = 5;
	vars.pplife = 50;
	vars.speed = 3;
	vars.sizex = 600;
	vars.sizey = 550;
	vars.offsetx = 0;
	vars.offsety = 50;
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	attributes.style = "display:block;position:absolute;"
	swfobject.embedSWF("/swf/bubbleLayer.swf", "flashBG1", "100%", "100%", "10.0.0","expressInstall.swf", vars, params, attributes);
	*/
	
	$(".about_mission_bg").append("<div id='flashBG2'/>")
	vars = {};
	vars.ppsec = 2;
	vars.pplife = 70;
	vars.speed = 3;
	vars.sizex = 100;
	vars.sizey = 350;
	vars.offsetx = 200;
	vars.offsety = 100;
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	attributes.style = "display:block;position:absolute;"

	
	swfobject.embedSWF("/swf/bubbleLayer.swf", "flashBG2", "100%", "100%", "10.0.0","expressInstall.swf", vars, params, attributes);

	$(".about_brand_bg").append("<div id='flashBG3'/>")
	vars = {};
	vars.ppsec = 5;
	vars.pplife = 50;
	vars.speed = 3;
	vars.sizex = 600;
	vars.sizey = 550;
	vars.offsetx = 0;
	vars.offsety = 50;
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	attributes.style = "display:block;position:absolute;"
	
	//attributes.style = "display:block;position:absolute;"
	swfobject.embedSWF("/swf/bubbleLayer.swf", "flashBG3", "100%", "100%", "10.0.0","expressInstall.swf", vars, params, attributes);
	
	
	$('.index_bg .song_container .song_list_container ul li.play_item').hover(
		function (){
			$(this).addClass('select');
		}, function (){
			$(this).removeClass('select');
		}
	);
	
	var shops = [];
	var citySelect = $('#region');
	var shopSelect = $('#shop');
	if(cities){
		for(var i in cities){
			shops[cities[i].City.id] = [];
			$('<option></option>').val(cities[i].City.id).text(cities[i].City.name).appendTo(citySelect);
/*			for(var j in cities[i].Shop){
				shops[cities[i].City.id].push({id:cities[i].Shop[j].id, name:cities[i].Shop[j].name});
				$('<option></option>').val(cities[i].Shop[j].id).text(cities[i].Shop[j].name).appendTo(shopSelect);
			}*/
		}
		citySelect.children('option:first').attr('selected', 'true');
//		shopSelect.children('option:first').attr('selected', 'true');
	}
	citySelect.change(function (){
		$('form.top_search .submit-btn').click();
/*		$('#shop>option:not(:first)').remove();
		for(var i in shops[$(this).val()]){
//			console.log(shops[$(this).val()][i]);
			$('<option></option>').val(shops[$(this).val()][i].id).text(shops[$(this).val()][i].name).appendTo(shopSelect);
		}
		shopSelect.children('option:first').attr('selected', 'true');
		selectBox($('#shop'));
		*/
	});
	
	if($('.city_search').length > 0){
		var citySelect = $('#city_region');
//		var shopSelect = $('#city_shop');
		if(cities){
			for(var i in cities){
				shops[cities[i].City.id] = [];
				$('<option></option>').val(cities[i].City.id).text(cities[i].City.name).appendTo(citySelect);
/*				for(var j in cities[i].Shop){
					shops[cities[i].City.id].push({id:cities[i].Shop[j].id, name:cities[i].Shop[j].name});
					if(!current_city || current_city == cities[i].City.id){
						$('<option></option>').val(cities[i].Shop[j].id).text(cities[i].Shop[j].name).appendTo(shopSelect);
					}
				}*/
			}
			if(!current_city){
				citySelect.children('option:first').attr('selected', 'true');
			}
			shopSelect.children('option:first').attr('selected', 'true');
		}
		citySelect.change(function (){
			if($('input#ShopKeyword').hasClass('empty')){
				$('input#ShopKeyword').val('');
			}
			$('#shopSearchForm .submit-btn').click();
/*			$('#shop>option:not(:first)').remove();
			for(var i in shops[$(this).val()]){
	//			console.log(shops[$(this).val()][i]);
				$('<option></option>').val(shops[$(this).val()][i].id).text(shops[$(this).val()][i].name).appendTo(shopSelect);
			}
			shopSelect.children('option:first').attr('selected', 'true');
			selectBox($('#city_shop'));
			*/
		});
	}
	
	
	
	if (!(navigator.userAgent.match(/iPhone/i)) && !(navigator.userAgent.match(/iPod/i))) {
		$("select").each(function(){
			selectBox($(this))
		})
	}
	
	/* Remove Below Code when Use CMS DATA*/
	$("#loginform").submit(function(){
		window.open('providelink.html');
		return false;
	});
	if($("#googleMap").length>=1){
		initialize( $('input[name=x_position]').val(), $('input[name=y_position]').val());
	}
	$(".colorbox").colorbox();
	
	if($('input#ShopKeyword').length > 0){
		$('input#ShopKeyword').focus(function (){
			if($(this).hasClass('empty')){
				$(this).val('');
				$(this).removeClass('empty');
			}
		});
		
		$('input#ShopKeyword').change(function (){
			if(!$(this).hasClass('empty') || $(this).val() != $(this).attr('emptyText')){
				if($(this).val() == ''){
					$(this).addClass('empty');
					$(this).val($(this).attr('emptyText'));
				}else{
					$(this).removeClass('empty');
				}
			}
				
		});
		
		$('input#ShopKeyword').blur(function (){
			if(!$(this).hasClass('empty') || $(this).val() != $(this).attr('emptyText')){
				if($(this).val() == ''){
					$(this).addClass('empty');
					$(this).val($(this).attr('emptyText'));
				}else{
					$(this).removeClass('empty');
				}
			}
		});
		
		if($('input#ShopKeyword').val() == ''){
			$('input#ShopKeyword').addClass('empty');
			$('input#ShopKeyword').val($('input#ShopKeyword').attr('emptyText'));
		}else{
			$('input#ShopKeyword').removeClass('empty');
		}
		
		$('input#ShopKeyword').parent('form').submit(function (){
			if($('input#ShopKeyword').hasClass('empty')){
				$('input#ShopKeyword').val('');
			}
		});
	}
	
	$('img').bind("contextmenu", function(e) {
		e.preventDefault();
	});
	
})

/*$(window).load(function(){	
	if($("object#flashMenu").length==0){
		alert("noFlash")
	}
})*/

//init mouse hover effect
function initBtn(obj,clickFunction,btnClass){
	if(!(Boolean(btnClass))){
		btnClass = 'select';
	}
	obj.hover(function(){
		$(this).addClass(btnClass);
	},function(){
		$(this).removeClass(btnClass);
	})
	if(Boolean(clickFunction)){
		obj.click(clickFunction);
	}
}


// Cycle Banner function
$(function() {
    $('.slideshow').cycle({
        fx:     'fade',
        speed:  'fast',
        timeout: 3000,
        pager:  '#nav',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#nav li:eq(' + (idx) + ') a';
        }
	
    });

    /*$('#direct').click(function() {
        $('#nav li:eq(2) a').triggerHandler('click');
        return false;
    });*/
   $('.news_box .content').cycle({
		fx:     'fade',
        speed:  'fast',
        timeout: 5000,
		pager:  '.news_box .left_menu ul',
		pagerEvent: 'mouseover',
		pauseOnPagerHover: true,
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '.news_box .left_menu ul li:eq(' + (idx) + ') a';
        }
	});
	$('.news_box .content .detail').hover(function(){
		$('.news_box .content').cycle('pause');
	},function(){
		$('.news_box .content').cycle('resume');
	});
	
	$('.news_box .left_menu ul li a').unbind('click');
	
	$(".btn-slide").click(function(){
		$("#panel").slideToggle("slow");
		$(this).toggleClass("active"); return false;
	});
	$("#panel").slideUp('fast',function(){$('.panel_init').removeClass('panel_init');});
	//alert(('.branch_detail_cycle .branch_img_cycle img').length);
	$('.branch_detail_cycle .branch_img_cycle').cycle({
		fx:     'fade',
        speed:  'fast',
        timeout: 3000,
		pager:  '.branch_detail_cycle .branch_detail_nav',
		prev:   '.branch_detail_cycle_content .prev', 
		next:   '.branch_detail_cycle_content .next', 
		pagerEvent: 'mouseover',
		pauseOnPagerHover: true,
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '.branch_detail_cycle .branch_detail_nav li:eq(' + (idx) + ') img';
        }
	});
});
jQuery(window).load(function(){
		
	var maxHeight = 0;
	
	$('#slidebox>li').each(function (){
		if($(this).height() > maxHeight){
			maxHeight = $(this).height();
		}
	});
	
	$('#slidebox>li').height(maxHeight);
	
	if($('#slidebox>li').length > 0){
		$('.news_detail').height(maxHeight);
	}
	
	
});

// jcarousel slide function
jQuery(document).ready(function() {
	
	if($('#slidebox>li').length <= 1){
		$('.jcarousel-prev').hide();
		$('.jcarousel-next').hide();
		$('.page_crollpane').hide();
	}else{
		for(var i = 1; i <= $('#slidebox>li').length; i++){
			$('.page_crollpane .page_box').append("<span><a href='javascript:void(0);'>" + i + "</a></span>");
		}
	}
	
    jQuery('#slidebox').jcarousel({
		scroll: 1,
		itemVisibleInCallback: {
            onAfterAnimation:  function (carousel, item, idx, state){
				$('.page_crollpane .page_box span.current').removeClass("current");
				$('.page_crollpane .page_box span').eq(idx-1).addClass("current");
			}
        },
        initCallback: function (carousel) {
			jQuery('.page_crollpane .page_box a').bind('click', function() {
				carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
				return false;
			})
		}
		
	});
	
});


$(function(){
	  	$("form .check").each(function(){
			myDummy = $(this)
			myDummy.wrap('<div class="jform_checking '+myDummy.attr("class")+'" name="'+myDummy.attr("id")+'"></div>').after('<div class="warning_message '+myDummy.attr("id")+'"></div>');
			myDummy.parent().removeClass("check");
			myDummy.focus(function(){
				$(this).parents(".jform_checking").find(".warning_message").html("");
			})
			//alert(this.nodeName)
		})
		$("form input:text").each(function(){
			var parent = $(this).parent() 
			var dummy = $("label[for='"+$(this).attr("id")+"'].tips")
			if(dummy.length>0){
				//dummy.hide();
				var target = $(this)
				target.after(dummy)
				dummy.css({
						"display":"block",
						"background-color":target.css("background-color"),
						"width":target.css("width"),
						"height":target.css("height"),
						"line-height":target.css("height"),
						"margin-top":target.css("margin-top"),
						"margin-left":target.css("margin-left"),
						"margin-bottom":target.css("margin-bottom"),
						"margin-right":target.css("margin-right"),
						"padding-top":target.css("padding-top"),
						"padding-left":target.css("padding-left"),
						"padding-bottom":target.css("padding-bottom"),
						"padding-right":target.css("padding-right"),
						"border-top-color":target.css("border-top-color"),
						"border-top-width":target.css("border-top-width"),
						"border-top-style":target.css("border-top-style"),
						"border-left-color":target.css("border-left-color"),
						"border-left-width":target.css("border-left-width"),
						"border-left-style":target.css("border-left-style"),
						"border-bottom-color":target.css("border-bottom-color"),
						"border-bottom-width":target.css("border-bottom-width"),
						"border-bottom-style":target.css("border-bottom-style"),
						"border-right-color":target.css("border-right-color"),
						"border-right-width":target.css("border-right-width"),
						"border-right-style":target.css("border-right-style")
					})
					
				if(!Boolean(target.attr("value"))){
					target.hide()
				}else{
					dummy.hide()
				}	
				dummy.click(function(){
					$(this).hide();
					target.show();
				})
				target.blur(function(){
					if(!Boolean($(this).attr("value"))){
						$(this).hide();
						dummy.show();
					}
				})
			};
		
		})	
		$("form").submit(function(e){
			$(this).find(".jform_checking").each(function(){
				//alert($(this).attr("class"))
				startChecking($(this))
			})
			//var success = true;
			var str=""
			$(this).find(".jform_checking .warning_message").each(function(){
				if($(this).children().length>0){
					str+=($(this).text()+"\n")	
					//success=false
				}
			})
			if(Boolean(str)){
				alert(str)
				return false;
			}else{
				return true;
			}
		})
})

function printWarningMessage(target,msg){
	if($(".warningMsg ."+target+"."+msg).length>0){
		$(".warning_message."+target).html($(".warningMsg ."+target+"."+msg).clone())
	}else if($(".warningMsg .default."+msg).length>0){
		$(".warning_message."+target).html($(".warningMsg .default."+msg).clone())
	}else{
		$(".warning_message."+target).html($(".warningMsg ."+msg).clone())
	}
}


function startChecking(target){
	//alert(target.attr("class"))
	
	var classArray = target.attr("class").split(" ")
	for(var i=0; i<classArray.length; i++){
		if(chickingFactory(target,classArray[i])){
			printWarningMessage(target.attr("name"),classArray[i])
		}
		/*switch(classArray[i]){
			case "empty": 					
					if(chickingFactory(target,"empty")){
						printWarningMessage(target.attr("name"),"empty")
					}
					break;
			
		}*/
	}
}

function chickingFactory(target,type){
	if(target.find("input").length > 0){
		var dummy = target.find('input[name!=""]')
		switch(dummy.attr("type")){
			case "radio":
								
				break;
			default: return dataValidation(dummy.attr("value"),type)
		}
	}
	if(target.find("textarea").length > 0){
		return dataValidation(target.find("textarea").attr("value"),type)
	}
}
function dataValidation(data,type){
	//alert(data+","+type)
	switch(type){
		case "empty":
				return Boolean(data)
			break;
		case "not_empty":
				return !Boolean(data)
			break;
		case "email":
				return !validationEmail(data)
			break;
		case "not_email":
				return validationEmail(data)
			break;
		case "number":
				regex=/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
				return !regex.test(data)
			break;
		case "not_number":
				regex=/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
				return regex.test(data)
			break;
		default:
				if((/length/).test(type)){
					var length = type.match(/\d+/);
					if((/equal/i).test(type)){
						if(data.length!=parseInt(length)){
							return true
						}
					}
				}
				//if(type.substr(0,("lenght").length)=="length"){
					
				//}
			break;
	}
	return false;
}

function validationEmail(input){
	var emailArray=input.split("@");
	if (emailArray.length != 2 || emailArray[0].length == 0 || emailArray[1].length ==0){
		return false;
	}
	var postArray=emailArray[1].split(".");
	if (postArray.length < 2) {
		return false;
	}
	for (var i=0; i<postArray.length; i++){
		if (postArray[i].length < 1){
			return false;
		}
	}
	var suffix=postArray[postArray.length-1];
	if (suffix.length < 2 || suffix.length > 3){
		return false;
	}
	return true;
}

function imenuSubmenu(id){
	if (id == 1) {
		if ($("#imenu .menu1").css("display") == "none") {
			$("#imenu .default").hide()
			$("#imenu .menu1").show()
		}else if ($("#imenu .default").css("display") == "none") {
			$("#imenu .default").show()
			$("#imenu .menu1").hide()
		}
	}
	if (id == 2) {
		if ($("#imenu .menu2").css("display") == "none") {
			$("#imenu .default").hide()
			$("#imenu .menu2").show()
		}else if ($("#imenu .default").css("display") == "none") {
			$("#imenu .default").show()
			$("#imenu .menu2").hide()
		}
	}
}

