// Make: 2011. 07. 13
// Author: Pearl Lee (songee@frameout.co.kr)

//Create HTML5 Elements
document.createElement('header');
document.createElement('footer');
document.createElement('section');
document.createElement('aside');
document.createElement('nav');
document.createElement('menu');
document.createElement('article');
document.createElement('address'); 

/* popup */
function popWin(url,wname,w,h,ptype,t,l) {
  var pWin, tVal, lVal;
  if (t == "c") {
    lVal = (screen.availWidth - w) / 2;
    tVal = (screen.availHeight - h) / 2;
  }
  else {
    tVal = (t!=null)?t:0;
    lVal = (l!=null)?l:0;
  }

  var popOpt = "width="+w+"px,height="+h+"px, top="+tVal+", left="+lVal;
  if(ptype == 0)
    popOpt += ",scrollbars=no";

  if(ptype == 1)
    popOpt += ",scrollbars=yes";

  if(ptype == 2)
    popOpt += ",scrollbars=yes,resizable=yes";

  pWin = window.open(url,wname,popOpt);
	//pWin.focus();

  return pWin;
}

/* main rolling event */
/*
	var rollFrameHeight = 1004; //rolling frame width
	var rollimgCount = 5; //rolling image count
	var viewTop = 0;
	var viewdefault = -(rollFrameHeight * rollimgCount);
	var lock = 0;
	var interval = null;

	function moveLeft()
	{
		if ( lock == 1 )
		{
			return;
		}

		lock = 1;
		viewTop += rollFrameHeight;

		J('.contWrap ul').animate({'left': viewTop + 'px'},
			function() {
				if ( viewTop == 0 )
				{
					viewTop = viewdefault;
					J('.contWrap ul').css('left', viewTop+'px');	
				}
				lock = 0;
			}
		);
	}
	function moveRight()
	{
		if ( lock == 1 )
		{
			return;
		}

		lock = 1;
		viewTop -= rollFrameHeight;

		J('.contWrap ul').animate({'left': viewTop + 'px'},
			function() {
				if ( viewTop == viewdefault*2)
				{
					viewTop = viewdefault;
					J('.contWrap ul').css('left', viewTop+'px');
				}
				lock = 0;
			}
		);
	}	
	*/
	
	var rollFrameHeight = 450; //rolling frame height
	var viewTop = 0;
	var viewdefault = 0;
	var lock = 0;
	var interval = null;

	/*function moveUp()
	{
		if ( lock == 1 )
		{
			return;
		}

		lock = 1;
		viewTop += rollFrameHeight;

		J('.contWrap ul').animate({'top': viewTop + 'px'},
			function() {
				if ( viewTop == 0 )
				{
					viewTop = viewdefault;
					J('.contWrap ul').css('top', viewTop+'px');	
				}
				lock = 0;
			}
		);
	}*/
	function moveDown()
	{
		if ( lock == 1 )
		{
			return;
		}

		lock = 1;
		viewTop -= rollFrameHeight;

		J('.contWrap ul').animate({'top': viewTop + 'px'}, 1500,
			function() {
				if ( viewTop == '-' + (rollFrameHeight * rollimgCount))
				{
					viewTop = viewdefault;
					J('.contWrap ul').css('top', viewTop+'px');
				}
				lock = 0;
			}
		);
	}	
	
	
	function gnbNavi(num)
		{
			J.get("/common/js/gnbNavi.js",
			function(data) { 
				if (J('article#main').css('display') != 'none') {
					J('article#main').slideUp(function() { 
						J(this).remove();
					});
				}
				
				J('#subContents').append(
					'<div class=\"scWrap\" idx=\"' + num + '\" style=\"height:' + data[num].contH + '\">' + data[num].cont + '</div>'
				);
				J('#subContents > .scWrap:last-child').prev().slideUp(function() {
					J(this).remove();
				});
				
			}
			, "json");
		}

J = jQuery.noConflict();
jQuery( function(J) {
	//ie flicker bug fix
	if (J.browser.msie) { try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {} }
	
	// PNGFIX
	J(document).pngFix({ blankgif:'blank.gif' });
	
	
	/* main rolling event */
	J('.contWrap ul').html( J('.mainGroup ul').html() + '<li class=\"fake\">' + J('.mainGroup li:first-child').html() + '</li>'); 
	J('.contWrap ul').css('height', (rollFrameHeight * rollimgCount) + rollFrameHeight);
	
	
	//J('.contWrap button').hide();
	//auto rolling event
	interval = setInterval(moveDown , 5000);
	J('.contWrap').mouseenter(function() {
		clearInterval(interval);
		interval = null;
		/*J(this).children('button').fadeIn();*/
	});
	J('.contWrap').mouseleave(function() {
		interval = setInterval(moveDown, 5000);
		/*J(this).children('button').fadeOut();*/
	});
	
	/* gnb navigation */
	
	J('header menu li').click(function() {
		if (J('.topCont').css('display') == 'none') {
			J('article.topCont').slideDown();
			J('.portWrap .inOut li:first-child').show();
			J('.portWrap .inOut li:last-child').hide();
		}
		var navIndex = J(this).index();		
		var scNum = J('#subContents .scWrap').attr('idx');
		if (navIndex == scNum) { return }
		gnbNavi( navIndex );
		
		if (J('article#main').css('display') != 'none') J('article#main').slideUp();
	});
	
	/* gnb navi, portfolio history click effect */
	J('.category li, .option li, menu li').click(function() {
		J(this).parent().children().removeClass('current');
		J(this).addClass('current');
	});
	
	/* portfolio launching img hover effect (use only ie6) 
	if (J.browser.msie && J.browser.version == "6.0") { 
		J('.launching li a').hover(function() {
			J(this).children('span').toggleClass('current');
		});
	}*/
	
	if (J.browser.msie) { 
		J('.inOut li button').hover(
			function() {
				J(this).toggleClass('current');
			}
		);
	}
	/* upper layer on and off effect */
	J('.inOut li:last-child').hide();
	J('.inOut li:first-child').click(function() {
		J('.contWrap').mouseenter(); //main rolling gallery stop
		J(this).blur(); //button focus remove
		if (J.browser.msie && (J.browser.version == "6.0" || J.browser.version == "7.0")) { //ie7, ie6 effect view bug
			if(J('.topCont #main').css('display') == 'block') {
				J('.topCont #main').slideUp(function() {
					J('.inOut li').toggle();
				});
			} 
			else {
				J('.topCont').slideUp(function() {
					J('.inOut li').toggle();	
				});
			}
		} 
		else {
			J('.topCont').slideUp(function() {
				J('.inOut li').toggle();
			});
		}
	});
	J('.inOut li:last-child').click(function() {
		J('.contWrap').mouseleave(); //main rolling gallery restart
		J(this).blur(); //button focus remove
		if (J.browser.msie && (J.browser.version == "6.0" || J.browser.version == "7.0")) { //ie7, ie6 effect view bug
			if(J('.topCont #main').css('display') == 'none') {
				J('.topCont #main').slideDown(function() {
					J('.inOut li').toggle();	
				});
			} 
			else {
				J('.topCont').slideDown(function() {
					J('.inOut li').toggle();	
				});
			}
		} 
		else {
			J('.topCont').slideDown(function() {
				J('.inOut li').toggle();	
			});	
		}
	});
	
	
	//footer
	J('a[href*=#top]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var Jtarget = J(this.hash);
			Jtarget = Jtarget.length && Jtarget || jQuery('[name=' + this.hash.slice(1) +']');
			if (Jtarget.length) {
				var targetOffset = Jtarget.offset().top;
				J('html,body').clearQueue().stop().animate({ scrollTop: targetOffset }, 1200);
				return false;
			}
		}
	});
});


