// JavaScript Document

var currentNum = 0;

var totalImg = 0;

var caseStudyList = [];

var archiveList=[];

var g_currentMenu = 0;

var isPlaying = false;

var years = [];

var typeArr = ['WEB', 'RIA', 'GUI', 'Mobile App', 'Motion'];

var indusArr = ['Education', 'Entertainment', 'Finance', 'e-Commerce', 'Fashion & Beauty', 'Corporation', 'Technology & Telecom', 'Leisure', 'Service', 'Automotive'];

var sortJpgArr = ['image/SortDate.jpg', 'image/SortType.jpg', 'image/SortIndus.jpg'];

var newsArr = [];

var currentPage = 0;

var currentSort;

var currentURL='http://www.kobalt60.com/';;

var sortText;

var scrollPosition = 0;

var scrollHeight = 0;

var maskHeight = 0;

var num = 0;

var scrollBar = 0;

var bodyScroll = false;

var marginNum = 0;

var downDiv = maskHeight;

$j = jQuery.noConflict();

jQuery().ready(function(){
	
	updateContentSize();
	
	var bgArr = ['bg.gif','bg_b.gif','bg_c.gif','bg_d.gif', 'bg-a.png', 'bg-e.png'];
	
	var bgNum = Math.floor(Math.random()*bgArr.length);
	
	if(bgNum == bgArr.length){
		bgNum = bgArr.length-1;
	}
	jQuery('body').css('background','url(image/'+bgArr[bgNum]+') repeat');
	jQuery('#wrap').show();

	jQuery.easing.def = 'easeInOutExpo';

	var urlText = '';

	mapClose();
	
	jQuery('.mapButton').hover(underlineShow, underlineHide);

	var TextType='sort=';

	urlText = String(window.location);

	if(urlText.indexOf(TextType) == -1){

		getSortStatus();

	}else{

		currentSort = urlText.substr(urlText.indexOf(TextType)+5,1);

		TextType = 'sortText=';

		sortText = urlText.substr(urlText.indexOf(TextType)+9,1);

	}

	jQuery('.thumbitem').hover(thumbOver, thumbOut);

	jQuery('.sortListItem1').hover(listOver0, listOut);

	jQuery('.sortListItem2').hover(listOver1, listOut);

	jQuery('.sortListItem3').hover(listOver2, listOut);

	jQuery('#slideImg').mouseover(removeBtn);

	if(urlText.indexOf('News') != -1){	
		jQuery('#newsContext').hover(function(){bodyScroll=true},function(){bodyScroll=false});

		var divarea = document.getElementById("newsContext");

		if (divarea.addEventListener) {            

			divarea.addEventListener('DOMMouseScroll', wheel, false);

		}

		if (window.addEventListener) window.addEventListener('DOMMouseScroll', onMouseWheel, false);

		window.onmousewheel = document.onmousewheel = onMouseWheel;

		divarea.onmousewheel = wheel;
		
		jQuery('.newsItem').hover(newsOver, newsOut);
		
		jQuery('#newsPlus').hover(plusOver, plusOut);
		if(navigator.appVersion.indexOf("iPhone") > -1){
			var newsHtml = '';
		
			newsHtml = jQuery('.newsScroll').html();

			jQuery('#newsContext').html(newsHtml);
			
			jQuery('.newsContents').css('position','relative');
			
			jQuery('#newsContext').css('overflow','visible');
			
			jQuery('#currentContent').css('margin-top','-110px');
			
			jQuery('#newsContext').css('padding-top', '210px');
			
			jQuery('#newsContext').css('position','relative');
			
		}else if(navigator.appVersion.indexOf("iPad") > -1){
			var newsHtml = '';
		
			newsHtml = jQuery('.newsScroll').html();

			jQuery('#newsContext').html(newsHtml);
			
			jQuery('.newsContents').css('position','relative');
			
			jQuery('#newsContext').css('overflow','visible');
			
			jQuery('#currentContent').css('margin-top','-110px');
			
			jQuery('#newsContext').css('padding-top', '210px');
			
			jQuery('#newsContext').css('position','relative');
		}else if(navigator.appVersion.indexOf("Android") > -1){
			var newsHtml = '';
		
			newsHtml = jQuery('.newsScroll').html();

			jQuery('#newsContext').html(newsHtml);
			
			jQuery('.newsContents').css('position','relative');
			
			jQuery('#newsContext').css('overflow','visible');
			
			jQuery('#currentContent').css('margin-top','-110px');
			
			jQuery('#newsContext').css('padding-top', '210px');
			
			jQuery('#newsContext').css('position','relative');
		}else{
		}

	}

	if(jQuery(window).height() < 800){

		jQuery('.newsLeft').css('min-height', jQuery('#wrap').height());

	}else{

		jQuery('.newsLeft').css('min-height', jQuery('#wrap').height());

	}

	TextType='page=';

	urlText = String(window.location);

	if(urlText.indexOf(TextType) == -1){

		currentPage = 0;

	}else{

		currentPage = urlText.substr(urlText.indexOf(TextType)+5,1);

	}
	if(jQuery('.ImgArray')){
		resizeImgContainer();
		jQuery('.ImgArray').load(resizeImgContainer);
	}
	//jQuery('.ImgArray').load(resizeImgContainer);

});

function plusOver(){
	jQuery('#newsPlus').attr('src','image/nextNewsOver.gif');
}

function plusOut(){
	jQuery('#newsPlus').attr('src','image/nextNews.gif');
}

function underlineShow(){
	jQuery(this).css('text-decoration','underline');
}

function underlineHide(){
	jQuery(this).css('text-decoration','none');
}

function newsOver(){
	if(jQuery(this).hasClass('selected')){
		
	}else{
		jQuery(this).addClass('newsOver');
	}
}

function newsOut(){
	if(jQuery(this).hasClass('selected')){
		
	}else{
		jQuery(this).removeClass('newsOver');
	}
}

function onMouseWheel(event)

{

	if (!event)

		event = window.event;



	if (bodyScroll) {

		return cancelEvent(event);

	}



	return true;

}



function ArchiveBack(){

	var sortData = [];
	
	var urlText = '';

	var TextType='sort=';

	urlText = String(window.location);
	
	sortData.push(urlText.substr(urlText.indexOf(TextType)+5,1));
	
	TextType='sortText=';
	
	sortData.push(urlText.substr(urlText.indexOf(TextType)+9,1));
	
	window.location = currentURL + 'archives.php?id=0&sort='+sortData[0]+'&sortText='+sortData[1];
}



function CaseStudyBack(){

	show(2);

}


function resizeImgContainer(){
	if(totalImg == 0){
		var num = 720 / jQuery('#slideImg iframe').width();
		
		var heightNum = jQuery('#slideImg iframe').height() * num;
		
		jQuery('#slideImg iframe').attr('width', '720px');
		
		jQuery('#slideImg iframe').attr('height', heightNum+'px');

		jQuery('.caseImgContianer').css('min-height',jQuery('#slideImg iframe').height());
		
		jQuery('.imgBtnContainer').css('margin-top', jQuery('.caseImgContianer').height() + 10);

	}else{
		var BtnMargin = Math.floor((jQuery('.ImgArray').height()-40)/2);
		
		jQuery('.caseImgContianer').css('min-height',jQuery('.ImgArray').height());

		jQuery('.imgleftBtn').css('min-height', String(jQuery('.ImgArray').height())+'px');

		jQuery('.imgrightBtn').css('min-height', String(jQuery('.ImgArray').height())+'px');
		
		jQuery('#leftSlide').css('min-height', String((jQuery('.ImgArray').height()-BtnMargin))+'px');

		jQuery('#rightSlide').css('min-height', String((jQuery('.ImgArray').height()-BtnMargin))+'px');
		
		jQuery('#leftSlide').css('padding-top', String(BtnMargin)+'px');
		
		jQuery('#rightSlide').css('padding-top', String(BtnMargin)+'px');
		 
		jQuery('.imgBtnContainer').css('margin-top', jQuery('.caseImgContianer').height() + 10);

	}

}


function cancelEvent(e)

{

	e = e ? e : window.event;

	if (e.stopPropagation)

		e.stopPropagation();

	if (e.preventDefault)

		e.preventDefault();

	e.cancelBubble = true;

	e.cancel = true;

	e.returnValue = false;

	return false;

}





function show(data) {

	var str = "";

	for (var key in data)

		str = str + key + " : " + data[key] + "\n";

	return str;

}
		

function handle(delta) {

	if(jQuery('#scrollBar').hasClass('view')){

		return

	}

	scrollHeight = jQuery('.newsScroll').height();

	maskHeight = jQuery('#newsContext').height()-100;

	num = maskHeight/scrollHeight;

	scrollBar = maskHeight*num;

	var move = delta*20;

	scrollPosition = scrollPosition + move;

	scrollPosition = Math.ceil(scrollPosition);

	if(scrollPosition > 0){

		scrollPosition = 0;

	}

	if(scrollPosition < marginNum*(-1)){

		scrollPosition = marginNum*(-1);

	}

	jQuery('.newsScroll').css('top',scrollPosition*(scrollHeight/maskHeight)+"px");

	jQuery('#scrollBar').css('top',scrollPosition*(-1)+"px");

}



function wheel(event){

	var delta = 0;

	if (!event) event = window.event;

	if (event.wheelDelta) {

		delta = event.wheelDelta/120;

	if (window.opera) delta = -delta;

	} else if (event.detail) {

		delta = -event.detail/3;

	}

	if (delta)

		handle(delta);

}



window.onresize = function(){

	updateContentSize();

	var move = (maskHeight-downDiv)*20;

	scrollPosition = scrollPosition + move;

	scrollPosition = Math.ceil(scrollPosition);

	if(scrollPosition > 0){

		scrollPosition = 0;

	}

	if(scrollPosition < marginNum*(-1)){

		scrollPosition = marginNum*(-1);

	}

	jQuery('.newsScroll').css('top',scrollPosition*(scrollHeight/maskHeight)+"px");

	jQuery('#scrollBar').css('top',scrollPosition*(-1)+"px");

}

window.onload=function() {

	updateContentSize();

	downDiv = maskHeight;
	
	resizeImgContainer();

}

function getSortStatus(){

	jQuery.ajax({

		type:'GET',

		url:'html/currentSort.php',

		dataType: "html",

		success:function(response){

			currentSort = response;

		}

	});

}



function mapClose(){

	jQuery('#mapDeem').hide();

}



function openMap(){

	jQuery('#mapDeem').show();

	if(jQuery('.mapData iframe')){

		html='<iframe width="670" height="670" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.kr/maps?q=%EC%BD%94%EB%B0%9C%ED%8A%B8%EC%8B%9D%EC%8A%A4%ED%8B%B0&amp;ie=UTF8&amp;t=m&amp;vpsrc=6&amp;ll=37.515377,127.037165&amp;spn=0.011403,0.014355&amp;z=16&amp;iwloc=A&amp;output=embed"></iframe><br /><table width="670px" cellspaing="0" cellpadding="0"><tr height="5px"><td></td><td></td><td></td></tr><tr><td width="55px"></td><td width="460px"></td><td width="155px"><a href="http://maps.google.co.kr/maps?q=%EC%BD%94%EB%B0%9C%ED%8A%B8%EC%8B%9D%EC%8A%A4%ED%8B%B0&amp;ie=UTF8&amp;t=m&amp;vpsrc=6&amp;ll=37.515377,127.037165&amp;spn=0.011403,0.014355&amp;z=16&amp;iwloc=A&amp;source=embed" target="_blank" class="viewGooglemap"><img src="image/icon_new_window.png"> View in Google map</a></td></tr></table>';

		jQuery('.mapData').html(html);

	}

}



function thumbOver(){
	jQuery.easing.def = 'easeOutExpo';
	
	jQuery(this).children().eq(0).children().eq(0).children().eq(0).stop();
	
	jQuery(this).children().eq(0).children().eq(0).children().eq(0).animate({"margin-left":"0px"});
	
	jQuery(this).children().eq(1).css('color', '#ff4800');

	jQuery(this).children().eq(2).css('color', '#ff4800');
	

	/*jQuery(this).children().eq(1).css('color', '#ff4800');

	jQuery(this).children().eq(2).css('color', '#ff4800');

	jQuery(this).children().eq(0).addClass('thumbImgOver');

	jQuery(this).children().eq(0).css('border', '3px solid #ff4800');*/

}



function thumbOut(){
	jQuery.easing.def = 'easeOutExpo';
	
	jQuery(this).children().eq(0).children().eq(0).children().eq(0).stop();

	jQuery(this).children().eq(0).children().eq(0).children().eq(0).animate({"margin-left":"-220px"});
	
	jQuery(this).children().eq(1).css('color', '#656565');

	jQuery(this).children().eq(2).css('color', '#999');

	/*jQuery(this).children().eq(1).css('color', '#656565');

	jQuery(this).children().eq(2).css('color', '#999');

	jQuery(this).children().eq(0).removeClass('thumbImgOver');

	jQuery(this).children().eq(0).css('border', '1px solid #CCC');*/

}



function showCaseStudy(idx){

	window.location = currentURL + 'casestudy.php?id='+idx;

}



function removeBtn(){
	jQuery.easing.def = 'easeOutExpo';
	
	jQuery('#leftSlide').fadeOut(500);

	jQuery('#rightSlide').fadeOut(500);

}



function slideImg(idx){
	if(totalImg == 1) return

	jQuery('.imgConBtn').attr('src', 'image/imgpan.png');

	if( idx < 0 ){

		idx = 0;

	}else if( idx >= totalImg){

		idx = totalImg-1;

	}

	currentNum = idx;
	jQuery.easing.def = 'easeInOutExpo';
	jQuery('#slideImg').animate({"left":idx*-720+"px"}, 'slow');

	jQuery('.imgBtnContainer table tr').children().eq(idx+1).children().eq(0).attr('src','image/selectImg.png');

}



function leftOver(){
	jQuery.easing.def = 'easeOutExpo';
	if(currentNum != 0){

		//jQuery('#leftSlide').attr('src', 'image/leftBtn.gif');
		//jQuery('#leftSlide').hide();
		jQuery('#leftSlide').fadeIn(500);

		jQuery('#leftSlide').css('cursor', 'pointer');

	}else{

		jQuery('#leftSlide').css('cursor', 'default');
		
		jQuery('.imgleftBtn').css('cursor', 'default');

	}

}



function leftOut(){
	jQuery.easing.def = 'easeOutExpo';
	jQuery('#leftSlide').fadeOut(500);
	//jQuery('#leftSlide').attr('src', 'image/nullBtn.png');

}



function rightOver(){
	jQuery.easing.def = 'easeOutExpo';

	if(currentNum != totalImg-1){

	//jQuery('#rightSlide').attr('src', 'image/rightBtn.gif');
	
	jQuery('#rightSlide').fadeIn(500);

	jQuery('#rightSlide').css('cursor', 'pointer');

	}else{

		jQuery('#rightSlide').css('cursor', 'default');

		jQuery('.imgrightBtn').css('cursor', 'default');

	}

}



function rightOut(){
	jQuery.easing.def = 'easeOutExpo';
	jQuery('#rightSlide').fadeOut(500);
//	jQuery('#rightSlide').attr('src', 'image/nullBtn.png');

}



function updateContentSize() {

	if(jQuery('#content')) {
		var curUrl = '';
		curUrl = window.location;
		if(curUrl == 'http://www.kobalt60.com/'){
			if(navigator.appVersion.indexOf("iPhone") > -1){
				jQuery('#currentContent').css('min-height', jQuery(window).height());
			}
			if(jQuery(window).height() > 900){
				jQuery('#currentContent').css('min-height', jQuery(window).height());
			}else{
				jQuery('#currentContent').css('min-height', '900px');
			}
		}

		downDiv = maskHeight;

		jQuery('#content').css('min-height', jQuery(window).height());

		jQuery('#wrap').css('min-height', jQuery(window).height());

		jQuery('#wrap').css('left', (jQuery(window).width()-1100)/2);

		if(jQuery(window).width() < 1100){

			jQuery('#wrap').css('left', 0);

		}
		if(navigator.appVersion.indexOf("iPhone") > -1){
			jQuery('.newsContents').css('min-height','800px');
			jQuery('#newsContext').css('min-height','800px');
		}else if(navigator.appVersion.indexOf("iPad") > -1){
			jQuery('.newsContents').css('min-height','800px');
			jQuery('#newsContext').css('min-height','800px');
		}else if(navigator.appVersion.indexOf("Android") > -1){
			jQuery('.newsContents').css('min-height','800px');
			jQuery('#newsContext').css('min-height','800px');
		}else{
			//jQuery('.newsContents').attr('height','auto');
			jQuery('.newsContents').attr('height',(jQuery(window).height()-160)+'px');
			jQuery('#newsContext').css('min-height' ,(jQuery(window).height()-160)+'px');
		}
		
		jQuery('.newsDownFog').css('top' ,(jQuery(window).height()-180)+'px');

		scrollHeight = jQuery('.newsScroll').height();

		maskHeight = jQuery('#newsContext').height()-100;

		num = maskHeight/scrollHeight;

		scrollBar = maskHeight*num;

		scrollBar = Math.ceil(scrollBar);

		if(scrollBar > scrollHeight-1){

			scrollBar = scrollHeight;

			jQuery('#scrollBar').addClass('view');

			jQuery('#scrollBar').css('display', 'none');

		}else{

			jQuery('#scrollBar').css('display', 'block');

			jQuery('#scrollBar').removeClass('view');

		}

		jQuery('#scrollBar').css('height', scrollBar);

		marginNum = maskHeight - scrollBar;
		jQuery('.newsLeft').css('min-height', jQuery('#wrap').height());

		if(jQuery('#mapDeem')){

			jQuery('#mapDeem').css('width', jQuery(window).width());

			jQuery('#mapDeem').css('min-height', jQuery(window).height());

			if(jQuery(window).height() < jQuery('#wrap').height()){

				jQuery('#mapDeem').css('min-height', jQuery('#wrap').height());

			}

			if(jQuery('#mapDeem').width() < 1100){

				jQuery('#mapDeem').css('width', 1100);

			}

		}

	}

}



function compareFuncYear(a, b)

{

	if (a.year > b.year)

		return 1;

	else if (a.year < b.year)

		return -1;

		

	return 0;

}





function compareFuncType(a, b)

{

	if (a.type > b.type)

		return 1;

	else if (a.type < b.type)

		return -1;

	else if (a.type == b.type)

	{

		return compareFuncYear(a, b);

	}

		

	return 0;

}



function show(idx) {

	jQuery('.menuitem').attr('rel','');

	jQuery('.menuitem').eq(idx-1).attr('rel', 'on');

	

	if (idx == 1) {

		window.location = currentURL;		

	} else if (idx == 2) {

		window.location = currentURL + 'casestudy.php?id=0';

	} else if (idx == 3) {

		var urlText = '';

		var TextType='sort=';

		urlText = String(window.location);

		if(urlText.indexOf(TextType) == -1){

			if(currentSort == 1){

				sortArchives(currentSort, 0);

			}else if(currentSort == 3){
				sortArchives(currentSort, 6);
			}else{

				sortArchives(currentSort, 1);

			}

		}else{

			if(currentSort == 1){

				sortArchives(urlText.substr(urlText.indexOf(TextType)+5,1), 0);

			}else if(currentSort == 3){
				sortArchives(urlText.substr(urlText.indexOf(TextType)+5,1), 6);
			}else{

				sortArchives(urlText.substr(urlText.indexOf(TextType)+5,1), 1);

			}

		}

	}else if (idx == 4) {

		location = currentURL + 'about.php?id=1';

	}else if (idx == 5) {

		showNews(0);

	}

	archiveList.sort(compareFuncType);

	for(var i = 0; i < archiveList.length; i++)

	{

		

	}

	

	g_currentMenu = idx;

}



function showNews(idx){

	currentPage = idx;

	var idname = 0;

	jQuery.ajax({

		type:'post',

		url:'newsContents.php?id='+currentPage,

		success:function(response){

			idname = response;

			location = currentURL + 'News.php?id='+idname;

		}

	});

	/*var html = '';

	var category = ['','[프로젝트]', '[알림]', '[기사]', '[소식]', '[수상]']

	var total = (currentPage+1)*16;

	html += '<div class="newsLeft"><ul id="newsContainer">';

	if(total > newsArr.length){

		total = newsArr.length;

	}

	for(var i = currentPage*16;i < total;i++){

		html += '<li class="newsItem" onClick="viewNews('+newsArr[i].no+')"><div class="newsDate">'+newsArr[i].date+'</div>';

		html += '<div class="newsSubject">'+category[newsArr[i].category]+' '+ newsArr[i].subject+'</div>';

		html += '</li>';

	}

	html += '<li class="newsPageBtn">';

	for(var p = 0;p < (newsArr.length/16);p++){

		html += '<img src="image/imgpan.png" onClick="showNews('+p+')" style="margin-left:2px;margin-right:2px;cursor:pointer;">';

	}

	html += '</li>';

	html += '</ul></div>';

	html += '<iframe class="newsContents" src="" width=510px height=100% frameborder=0 scrolling=auto></iframe>';

	jQuery('#currentContent').html(html);

	jQuery('.newsPageBtn').children().eq(idx).attr('src', 'image/selectImg.png');

	viewNews(newsArr[currentPage*16].no);*/

}



function viewNews(idx){

	location = currentURL + 'News.php?id='+idx;

	/*var html = '';

	for(var i = 0; i < newsArr.length;i++){

		if(newsArr[i].no == idx){

			jQuery('li.newsItem').removeClass('newsSelect');

			jQuery('li.newsItem').children().removeClass('newsSelected');

			jQuery('#newsContainer').children().eq(i%16).addClass('newsSelect');

			jQuery('#newsContainer').children().eq(i%16).children().addClass('newsSelected');

			jQuery('.newsContents').attr('src', 'newsContents.php?id='+newsArr[i].no);

		}

	}*/

}



function sortArchives(Sort, idx){

	currentSort = Sort;

	window.location = currentURL + 'archives.php?id=0&sort='+Sort+'&sortText='+idx;

}



function sortData(idx){

	if(idx == 1){

		window.location = currentURL + 'archives.php?sort='+idx+'&sortText=0&id=0';

	}else if(idx == 3){
		window.location = currentURL + 'archives.php?sort='+idx+'&sortText=6&id=0';
	}else{

		window.location = currentURL + 'archives.php?sort='+idx+'&sortText=1&id=0';

	}

	currentSort = idx;

}



function listOver0(){

	jQuery('.sortListContainer').children().eq(0).addClass('sortOver');

	if(currentSort == 1){

		jQuery('.sortListContainer').children().eq(0).children().eq(0).attr('src','image/currentOverSort.png');

	}

}



function listOver1(){

	jQuery('.sortListContainer').children().eq(1).addClass('sortOver');

	if(currentSort == 2){

		jQuery('.sortListContainer').children().eq(1).children().eq(0).attr('src','image/currentOverSort.png');

	}

}



function listOver2(){

	jQuery('.sortListContainer').children().eq(2).addClass('sortOver');

	if(currentSort == 3){

		jQuery('.sortListContainer').children().eq(2).children().eq(0).attr('src','image/currentOverSort.png');

	}

}



function listOut(){

	for(var i=0;i<3;i++){

		if(i == currentSort-1){

			jQuery('.sortListContainer').children().eq(i).children().eq(0).attr('src','image/currentSort.png');

		}

		jQuery('.sortListContainer').children().eq(i).removeClass('sortOver');

	}

}



function SortMenuOpen(){

	jQuery('#sortMenu').show();

}



function showArchive(idx){

	window.location =currentURL+'archives.php?sort='+currentSort+'&sortText='+sortText+'&id='+idx;

}



function aboutShow(idx){

	window.location = currentURL + 'about.php?id='+idx;

}





function submitProject() {

	var title = jQuery("input[name='title']").val();

	var date = jQuery("input[name='date']").val();

	var desc = jQuery('#desc').val();

	

	jQuery.ajax({

		type:'post',

		url:'submitProject.php',

		data:{title:title,date:date,desc:desc},

		success:function(response){

			if (response == 'OK')

				alert('OK');

			else

				alert('ERROR');

		}

	});

}


