$(document).ready(function() {
	if ($.browser.msie && $.browser.version == '6.0') {
		firstChild();
		
	}
});


function firstChild(){
	var firstClassArr=new Array (
		".movieList1 li",
		".popList li",
		'.paging span a'
	);
	for (var i=0; i< firstClassArr.length ;i++ ){
		$(firstClassArr[i]+":first-child").addClass("first-child");
	}
}


$(document).ready(function(){
	$(".rollover").hover(
		function() {
			if($(this).attr("src").indexOf(".png") < 0){
				imgType = ".gif";
			}else{
				imgType = ".png";
			}
			var overType=($(this).attr("src").indexOf("_on"+imgType) > 0) ? "on"+imgType : "_on"+imgType;

			if ($(this).attr("src").indexOf(overType) > 0){
				$(this).addClass ("over");
			}else{
				var currentImg = $(this).attr("src").replace(imgType, overType);
				$(this).attr("src", currentImg);
			}

		}, function() {
			var overType=($(this).attr("src").indexOf("_on"+imgType) > 0) ? "_on"+imgType : "on"+imgType;
			if($(this).hasClass("over")!=true){
				var currentImg = $(this).attr("src").replace(overType, imgType);
				$(this).attr("src", currentImg);
			}
		}
	)
});


$('.iconMenu').ready(function() {
	$('.iconMenu li a').hover(
		function() {
			$('.iconMenu li a').css('backgroundPosition', '0 0');
			$(this).css('backgroundPosition', '0 -99px');
		},
		function() {
			$('.iconMenu li a').css('backgroundPosition', '0 0');
		}
	);
});


$(document).ready(function() {
	var h = $('.region ul').height();

	$('.region').mouseover(function() {
		$('.region ul').css('paddingTop', '10px')
		$('.region ul').stop().animate({'height': '145px'}, 200)
	})

	$('.region').mouseout(function() {
		$('.region ul').stop().animate({'height':'0px'}, 200, function() {
			$('.region ul').css('paddingTop', '0px')
		})
	})
});


$(document).ready(function() {
	$('#widget').hover(
		function() {$(this).stop(true).animate({'height' : '100px'}, 100)},
		function() {$(this).stop(true).delay(1000).animate({'height' : '20px'},200)}
	)

	$('#widget li a.menuTit').addClass('on')
	$('#widget li a').not('.menuTit').hover(
		function() {$(this).addClass('on');},
		function() {$('#widget li a').not('.menuTit').removeClass('on');}
	);

	
	$('.topList li a').hover(
		function() {$(this).addClass('on')},
		function() {$('.topList li a').removeClass('on')}
	)
});


$('.about').ready(function() {

	$('.picture li a').live('click', function() {
		var imgSrc = $(this).attr('href')
		$('.galleryPop img').attr('src', imgSrc);

		$('.galleryPop img').bind('load', function() {
			var picW = -$('.galleryPop').width()/2;
			var picH = -$('.galleryPop').height()/2;

			$('.galleryPop').css({
				'margin-left' : picW-30+'px',
				'margin-top' : picH
			});
			$('.galleryPop').fadeIn('slow');
		});
		return false;
	});

	$('.picture li a:last').trigger('click');

})


$('.downList').ready(function() {
	$('.download > div').hide();
	$('.download > div:first').show();

	$('.downList li a').click(function() {
		var index = $('.downList li a').index(this);
		var listH = $('.downList').height();
		var position = -(listH * index) + 'px';


		if(index == 0) {
			movieChange1();
		} else {
			$('#video .skin').empty();
		}

		$('.downList li a').parents('.downList').css({'background-position' : '0 ' + position});

		$('.download > div').hide();
		$('.download > div').eq(index).show();
	});
	return false;
})


function movieChange1(){
	$('#video .skin').remove();
	$('#video').prepend('<div class="skin"><object style="height: 231px; width: 277px"><param name="movie" value="http://www.youtube.com/v/9G8u3EMvE_Q?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/9G8u3EMvE_Q?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="277" height="231"></object></div>')
}


$('#moveTop').ready(function() {
	moveTop();
	$(window).scroll(function() {
		moveTop();
	});
});

function moveTop() {
	var start = 250;
	var positionH = $(window).scrollTop();
	var bannerH = positionH + start+'px';
	$('#moveTop').stop().animate({'top' : bannerH}, 800)
}


$('.portFolioList').ready(function() {
	$('.portFolioList li a').hover(
		function() {
			$(this).parents('.portFolioList').find('a').removeClass('on');
			$(this).addClass('on');
			$(this).next().show();
		},
		function() {
			$(this).parents('.portFolioList').find('a').removeClass('on');
			$(this).parents('.portFolioList').find('p').hide();
		}
	);
});


$(document).ready(function() {
	
	$('.movieTab li:first').addClass('on');
	$('.movie li').hide();
	$('.movie li:first').show();

	$('.movieTab li a').click(function() {
		var movieNo = $('.movieTab li a').index(this);

		$('.movieTab li').removeClass('on');
		$(this).parent().addClass('on');
		return false;
	});

	
	$('.popList a').click(function() {
		var imgUrl = $(this).attr('href');
		$('.popHigh').remove();
		$('#cntWrap').prepend('<div class="popHigh"><span><img src="" alt="" /></span></div>');
		$('.popHigh').prepend('<a href="#" class="popClose"><img src="/global/images/work/popClose.gif" alt="close" /></a>');

		$('.popHigh span img').attr('src', imgUrl);

		$('.popHigh img').bind('load', function() {
			$('.popHigh').show();
			$('.popHigh').css('top', $('#moveTop').position().top)
			var popHighW = -$('.popHigh span img').width()/2;
			var popHighH = -$('.popHigh span img').height()/2;

			$('.popHigh').css({
				'margin-left' : popHighW
				
			});
		});
		return false;
	});


	
	$('.popHigh').live('click', function() {
		$('.popHigh').hide();
		$('.popHigh span img').removeAttr('src')
		return false;
	});
});


$('.popHigh').ready(function() {
	$(window).scroll(function() {
		var start = 200;
		var positionH = $(window).scrollTop();
		var bannerH = positionH + start+'px';
		$('.popHigh').stop().animate({'top' : bannerH}, 800)
	});
})


$(document).ready(function() {
	var movFirst = $('.movieTab li a:first').attr('href');
	movieChange(movFirst);
})

function movieChange(movUrl){
	$('.listMovie').empty().append('<object style="height: 231px; width: 277px"><param name="movie" value='+movUrl+' ><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src='+movUrl+' type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" wmode="transparent" width="277" height="231"></object>')
}

$(document).ready(function() {
	var num = 3;
	var timeGap = 3000;
	var speed =500;
	var max = $('.slide li').length;
	var listW = $('.slide li').outerWidth();

	var ulWrap = listW * max;
	var direct = 'right'

	$('.slide').append('<a href="#" class="btn left"><img src="/global/images/common/btn/prev.png" class="png" alt="pre" /></a>');
	$('.left').after('<a href="#" class="btn right"><img src="/global/images/common/btn/next.png" class="png" alt="next" /></a>');

	$('.slide ul').wrap('<div class="listWrap"></div>');
	$('.slide .listWrap').css({
		'width' : listW * num,
		'overflow' : 'hidden',
		'position': 'relative',
		'margin' : '0 auto'
	});

	$('.slide ul').css({'width' : ulWrap});

	$('.slide ul li a').hover(
		function() {
			$('.slide ul li a').find('strong.over').remove();
			$(this).prepend('<strong class="over"></strong>')
		},
		function() {
			$('.slide ul li a').find('.over').remove();
		}
	);

	$('.slide .left').bind('click', function() {
		$('.slide ul').css({'marginLeft': -listW})
		$('.slide ul').prepend($('.slide ul li').eq(-1).clone(true));
		$('.slide ul').stop().animate({marginLeft: 0}, speed)
		$('.slide ul li').eq(-1).remove();
		clearTimeout(time);
		//time = setTimeout(moveEvent, timeGap);
		direct = 'left';
		return false;
	});

	$('.slide .right').bind('click', function() {
		$('.slide ul').stop().animate({marginLeft: -(listW)}, speed, function() {
			$('.slide ul').append($('.slide ul li').eq(0).clone(true));
			$('.slide ul li').eq(0).remove();
			$('.slide ul').css('marginLeft', '0');
			//clearTimeout(time);
			//time = setTimeout(moveEvent, timeGap);
			direct = 'right';
		})
		return false;
	});

	function moveEvent() {
		//time = setTimeout(moveEvent, timeGap);
		$('.slide .' + direct).trigger('click');
	};

//	$('.slide ul').hover(
//		function() {clearTimeout(time);},
//		function() {moveEvent();}
//	);
	//setTimeout(moveEvent, timeGap);
})


$('.highList').ready(function() {
	$('.highList li a').hover(
		function() {$(this).prepend('<strong></strong>')},
		function() {$('strong').remove()}
	);
});

$(document).ready(function() {

	$('.people ul li a').hover(
		function() {
			var ppNo = $('.people ul li a').index(this)+1;
			//alert(ppNo)
			$('.people ul').removeAttr('class');
			$('.people ul').addClass('people'+ppNo);

			//$(this).parents('ul').css({'background-image' : '/global/images/people/people_0'+ppNo+'.gif'})
		},
		function() {
			$('.people ul').removeAttr('class');
			$('.people ul').addClass('people0')
		}
	);
});

$(document).ready(function() {

	$('.people_en ul li a').hover(
		function() {
			var ppNo = $('.people_en ul li a').index(this)+1;
			//alert(ppNo)
			$('.people_en ul').removeAttr('class');
			$('.people_en ul').addClass('people'+ppNo);

			//$(this).parents('ul').css({'background-image' : '/global/images/people/people_0'+ppNo+'.gif'})
		},
		function() {
			$('.people_en ul').removeAttr('class');
			$('.people_en ul').addClass('people0')
		}
	);
});


$(document).ready(function(){
	/* work : portfolio */
	$('.portFolioList li a').bind('click', function(){
		openDialog($(this).attr('href'));
		return false;
	});

	/* people: 인물소개 */
	$('.people li a').bind('click', function(){
		openDialog1($(this).attr('href'));
		return false;
	});
	$('.people_en li a').bind('click', function(){
		openDialog1($(this).attr('href'));
		return false;
	});

	/* company: history */
	$('.about .history').bind('click', function(){
		openDialog($(this).attr('href'));
		return false;
	});

	/* solution: idea */
	$('.engineering .works1').bind('click', function(){
		openDialog($(this).attr('href'));
		return false;
	});

	/* solution: engineering */
	$('.engineering .works2').bind('click', function(){
		openDialog($(this).attr('href'));
		return false;
	});

	/* utils: roughMap */
	$('.roughMap').bind('click', function(){
		openDialog($(this).attr('href'));
		return false;
	});
});


$(document).ready(function() {
	var i  = 0;
	var listMax = $('.portFolioList li').length;
	//('.portFolioList li').hide();

	function moveEvent() {
		time = setTimeout(moveEvent, 100);
		$('.portFolioList li').eq(i).fadeIn();
		i = i + 1;

		if(i == listMax) {
			clearTimeout(time)
		}
	};
	
	moveEvent();
})


$(document).ready(function() {
	$('.globalMap').click(function() {
		$('.people').after('<div id="management"></div>')
		$('.people_en').after('<div id="management"></div>')
		$('#management').load('peopleMap.jsp');
		$('#management').fadeIn('slow');
	});

	$('#management .close').live('click', function() {
		$(this).parent().fadeOut('slow')
	});
});


$(document).ready(function() {
	$('.officeLocation a').hover(
		function() {
			$(this).find('img').show();
			$(this).css('zIndex', '10');
		},
		function() {
			$(this).find('img').hide();
			$(this).css('zIndex', '7');
		}
	);
})


$(document).ready(function() {
	$('#management a.man').live('click' ,function(){

		var infoW = -$(this).next().width()/2; 
		var infoH = -$(this).next().height()/2; 

		$('#management .infoPop').fadeOut();

		$('#management .infoPop .close').remove();
		$('#management .infoPop').prepend('<a href="#" class="close"><img src="/global/images/people/popClose.gif" class="clsoe" alt="close" /></a>'); 

		
		$('#management .infoPop a.close').click(function() {
			$('.man').css({'background-position' : '0 bottom'});
			$(this).parent().fadeOut();
		})

		$('a.man').css({'background-position' : '0 bottom'});
		$(this).css({'background-position' : '0 0'});
		$(this).next().css({'margin-top' : infoH, 'margin-left' : infoW}).fadeIn(); 

		
		$(this).next().click(function() {
			$(this).fadeOut();
			$('a.man').css({'background-position' : '0 bottom'});
		})
		return false;
	});
	return false;
});

$('#worldMap').ready(function() {
	var tabClass = "all";

	$('.tabStyle1 a').bind('click', function() {
		tabClass = $(this).attr('class');

		if(tabClass == 'rocal') {
			$('a.bulb').css({'background-position' : '0 0'});
			$('a.bulb').filter('.partners').css({'background-position' : '0 bottom'});
		}
		else if(tabClass == 'partners') {
			$('a.bulb').css({'background-position' : '0 0'});
			$('a.bulb:not(.partners)').css({'background-position' : '0 bottom'});
		}
		else {
			$('a.bulb').css({'background-position' : '0 0'});
		}
		tabClass
		return false;
	});

	
	$('.tabStyle1 a').each(function(i) {
		$('.tabStyle1 img').bind('click', function() {
			$('.tabStyle1 img').eq(i).attr('src', $('.tabStyle1 img').eq(i).attr('src').replace('_on', '_off'));
			$(this).attr('src', $(this).attr('src').replace('_off', '_on'));
			$('.infoPop').animate({'margin-left' : '0px'}, 300).fadeOut();
		});
	});

	$('#worldMap a.bulb').live('click' ,function(){
		var posT = $(this).position().top; 
		var posL = $(this).position().left; 
		var minPos = 590; 
		var movement = 23; 
		var speed = 400; 

		$('.infoPop').prepend('<a href="#" class="close"><img src="../images/network/close.gif" alt="close" /></a>'); 

		
		$('.infoPop a.close').click(function() {
			$('.bulb').css({'background-position' : '0 bottom'});
			$(this).parent().animate({'margin-left' : '0px'}, 300).fadeOut();

			$('.tabStyle1 a.' + tabClass).trigger('click');
			$('.tabStyle1 a.' + tabClass).find('img').trigger('click');
			$('.close').remove()
			return false;
		})

		$('.infoPop').animate({'margin-left' : '0px'}, speed).fadeOut();
		$('a.bulb').css({'background-position' : '0 bottom'});
		$(this).css({'background-position' : '0 0'});

		if(posL > minPos) {
			posL = posL - $(this).next().width() - movement+'px';
		}

		
		if($(this).attr('class') != 'bulb partners') {
			$(this).next().fadeIn().css({'top' : posT, 'left' : posL}).animate({'margin-left' : movement+'px'}, speed);
		} else {
			$(this).next().fadeIn().css({'top' : '50px', 'left' : '50px'}).animate({'margin-left' : movement+'px'}, speed);
		}
		return false;
	});
	return false;
});


$('.tabStyle1.solution').ready(function() {
	$('.serviceCnt').hide()
	$('.serviceCnt:first').show();

	$('.tabStyle1.solution li').each(function(i) {
		$('.tabStyle1.solution li a').click(function() {
			var tabId1 = $(this).attr('href').split('#')[1];
			var index = $('.serviceCnt li a').index(this);

			$('.serviceCnt').hide();
			$('#'+tabId1).show();

			$('.tabStyle1.solution li a img').eq(i).attr('src', $('.tabStyle1.solution li a img').eq(i).attr('src').replace('_on', '_off'));
			$(this).find('img').attr('src', $(this).find('img').attr('src').replace('_off', '_on'));
			return false;
		})
	});
});


function randomImg() {
	i = Math.round(Math.random()*2)
	var randomImg = '/global/images/company/phil_visual'+i+'.gif'
	$('.visual img').attr('src', randomImg)
}


function openDialog(path) {
	$('#wrapper').append('<div class="dialog_frame"></div>');
	loadDialog(path);
}

function openDialog(path, formObj) {

	$('#wrapper').append('<div class="dialog_frame"></div>');
	loadDialog(path, formObj);
}
function openDialog1(path, formObj) {

	$('#wrapper').append('<div class="dialog_frame"></div>');
	loadDialog1(path, formObj);
}

function loadDialog(path) {
	$.get(path, loadDialogCallback);
}

function loadDialog(path, formObj) {
	$.post(path, $(formObj).serialize(), loadDialogCallback, "html");
}
function loadDialog1(path, formObj) {
	$.get(path, $(formObj).serialize(), loadDialogCallback, "html");
}

function loadDialogCallback(data) {
	var source = data;
	data = data.replace(/<script.*>.*<\/script>/ig,""); // Remove script tags
	data = data.replace(/<\/?meta.*>/ig,""); //Remove link tags
	data = data.replace(/<\/?link.*>/ig,""); //Remove link tags
	data = data.replace(/<\/?html.*>/ig,""); //Remove html tag
	data = data.replace(/<\/?body.*>/ig,""); //Remove body tag
	data = data.replace(/<\/?head.*>/ig,""); //Remove head tag
	data = data.replace(/<\/?!doctype.*>/ig,""); //Remove doctype
	data = data.replace(/<title.*>.*<\/title>/ig,""); // Remove title tags
	$('.dialog_frame').empty().html(data);
	$('.dialog_frame style').clone().appendTo('head').attr('id','dialog_frame');
	$('.dialog_frame').remove();

}

/* alert */
var dialog = {
	addElem : '<div class="alertBox" ><div class="msgCnt"><p class="txtMsg"></p><div class="alertBtn"></div></div></div>',
	defaultAlert : function(msg, callbackFunc) {
		$('#wrapper').append(this.addElem)
			.find('.txtMsg').html(msg)
			.end().find('.alertBtn').html('<span class="confirm"><img src="/resources/global/images/common/btn/confirm.gif" alt="확인" /></span>');
		$('.alertBox').dialog({
			bgiframe: true,
			//dialogClass:'alert_typeA',
			minHeight:140,
			height:'auto',
			minWidth:370,
			modal: true,
			resizable:false
		})
		$('.confirm').click(function() {
			if (typeof callbackFunc == 'function') {
				callbackFunc(true);
			}
			$('.alertBox').dialog('destroy');
		});
		$('.cancel').click(function() {
			if (typeof callbackFunc == 'function') {
				callbackFunc(false);
			}
			$('.alertBox').dialog('destroy');
		});
	},
	confirmAlert : function(msg, callbackFunc) {
		$('#wrapper').append(this.addElem)
			.find('.txtMsg').html(msg)
			.end().find('.alertBtn').html('<span class="confirm"><img src="/resources/global/images/common/btn/yes.gif" alt="확인" /></span><span class="cancel"><img src="/resources/global/images/common/btn/no.gif" alt="취소" /></span>');
		$('.alertBox').dialog({
			bgiframe: true,
			dialogClass:'alert_typeA',
			minHeight:140,
			height:'auto',
			minWidth:370,
			modal: true,
			resizable:false
		})
		$('.confirm').click(function() {
			if (typeof callbackFunc == 'function') {
				callbackFunc(true);
			}
			$('.alertBox').dialog('destroy');
		});
		$('.cancel').click(function() {
			if (typeof callbackFunc == 'function') {
				callbackFunc(false);
			}
			$('.alertBox').dialog('destroy');
		});
	},
	customAlert : function(msg, button1, button2, callbackFunc) {
		$('#wrapper').append(this.addElem)
			.find('.txtMsg').text(msg)
			.end().find('.alertBtn').html('<span class="confirm">'+ button1+'</span><span class="cancel">'+button2+'</span>');
		$('.alertBox').dialog({
			bgiframe: true,
			dialogClass:'alert_typeA',
			minHeight:140,
			height:'auto',
			minWidth:370,
			modal: true,
			resizable:false
		})
		$('.confirm').click(function() {
			if (typeof callbackFunc == 'function') {
				callbackFunc(true);
			}
			$('.alertBox').dialog('destroy');
		});
		$('.cancel').click(function() {
			if (typeof callbackFunc == 'function') {
				callbackFunc(false);
			}
			$('.alertBox').dialog('destroy');
		});
	}
};


function pagePrint(Obj) { 
	  var W = 830;
	  var H = 850; 
	
	  var features = "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=" + W + ",height=" + H + ",left=0,top=0"; 
	  var PrintPage = window.open("about:blank",Obj.id,features); 
	
	  PrintPage.document.open(); 
	  PrintPage.document.write("<html><head><title>WORLDWIDE</title><link rel='stylesheet' type='text/css' href='/global/common/css/print.css' /></head><body><div id='contents'>" + Obj.innerHTML + "</div></body></html>"); 
	  PrintPage.document.close(); 
	
	  PrintPage.document.title = document.domain; 
	  PrintPage.print(PrintPage.location.reload()); 
	}


