var moreItemPage=1;
var winFullId;
var winFullIdNum;
$(document).ready(function(){

	try{
		$('#imgWrap').find('img').load(function(){
			if(!resize_())
			$('#imgWrap').find('img').attr('src',$('#imgWrap').find('img').attr('src'));
		});
		
		if($('#imgListsWarp').find('.imgBox').size()){
			for(var i=0;i<6;i++){
				$('#imgListsWarp').find('.imgBox').eq(i).find('img').attr('src',$('#imgListsWarp').find('.imgBox').eq(i).find('img').attr('value'));
			}
		}
		if($('#imgListsWarp').find('.imgBox').size()>6){
			moreItemFun();
		}
		btnclose();
		popDoDiv();
		popDoDivFull();
		aboutLink();
		foo_join();
		artList();
		joinBtn();
		foo_Tearm();
		resize_();
		makeTale();
	}catch(err){}
	
});

$(window).resize(function() {
	 resize_();
});

$(function(){
	try{
		$('.popwin').css('display','block');
		$('.scroll-pane').jScrollPane({showArrows:true});	
		$('.popwin').hide();
	}catch(err){}
		
});

if(jQuery.browser.msie){
	$('#imgWrap').find('img').src=$('#imgWrap').find('img').src+'?abc';
}

function resize_(){
try{
	if($('#imgWrap').find('img').size()){
		var w=$('#imgWrap').find('img').width();
		var h=$('#imgWrap').find('img').height();
		var w1=$('#imgWrap').width();
		var h1=$('#imgWrap').height();
		if((h*w1 / w) > h1){
			$('#imgWrap').find('img').width(w1);
			$('#imgWrap').find('img').height(h*w1/w);
		}else{
			$('#imgWrap').find('img').height(h1);
			$('#imgWrap').find('img').width(w*h1/h);
		}
		$('#imgWrap').find('img').show();
		return true;
	}
}catch(err){}
};

function makeTale(){
	if(tabList.length){
		var txt='';
		for(var i=0;i<tabList.length;i++){
			txt += "<div id='"+URLdecode(tabList[i][0])+"'"+' class="popDoDivFullSocure" ><a href="#" class="closeDOMWindow">'+URLdecode(tabLang[0]);
			txt += '</a><table class="tableDOMWindow" width="800" height="466" border="0" cellspacing="0" cellpadding="0"><tr><td valign="middle" align="center">';
			txt += '<img src="/images/loading.gif" value="'+URLdecode(tabList[i][1])+'"/>';
			txt += '</td></tr></table><div class="wrapDOMWindows"><table width="690" height="66" border="0" cellspacing="0" cellpadding="0" class="tableInfoDOMWindow"><tr><td valign="top" align="left" width="62">';
			txt += URLdecode(tabLang[1])+'</td><td valign="top" align="left" width="154"><strong>'+URLdecode(tabList[i][2])+'</strong></td><td valign="top" align="left" width="81">';
			txt += URLdecode(tabLang[2])+'</td><td valign="top" align="left" width="128" ><strong>'+URLdecode(tabList[i][3])+'</strong></td><td valign="top" align="left" width="48">';
			txt += URLdecode(tabLang[3])+'</td><td width="217" valign="top" align="left" ><strong>'+URLdecode(tabList[i][4])+'</strong></td></tr><tr><td valign="top" align="left" width="62">';
			txt += URLdecode(tabLang[4])+'</td><td valign="top" align="left" width="154"><strong>'+URLdecode(tabList[i][5])+'</strong></td><td valign="top" align="left" width="81">';
			txt += URLdecode(tabLang[5])+'</td> <td valign="top" align="left" width="128" ><strong>'+URLdecode(tabList[i][6])+'</strong></td><td valign="top" align="left" colspan="2"><a href="#" onclick="mailToi(';
			txt += '\''+URLdecode(tabList[i][7])+'\',\''+URLdecode(tabList[i][8])+'\',\''+URLdecode(tabList[i][9])+'\')" class="btnEnquire">'+URLdecode(tabLang[6]);
			txt += '</a></td></tr><tr><td valign="top" align="left" width="62">';
			txt += URLdecode(tabLang[7])+'</td><td valign="top" align="left" width="154"><strong>'+URLdecode(tabList[i][10])+'</strong></td><td valign="top" align="left" width="81">';
			txt += URLdecode(tabLang[8])+'</td> <td valign="top" align="left" width="128" ><strong>'+URLdecode(tabList[i][11])+'</strong></td><td valign="top" align="left" colspan="2">&nbsp;</td></tr></table>';
			txt += '<div class="moreItemDom"><a href="#" class="preItemDom"></a><a href="#" class="nextItemDom"></a></div></div></div>';
			
		}
		$('body').append(txt);
	}
}

function mailToi(to,subject,bodyi){
	if(to){
		var mailto = 'mailto:'+to;
		if(subject)
			mailto += '?subject=' + URLdecode(subject);
		if(subject && bodyi)
			mailto += '&body=' + URLdecode(bodyi);
		else if(bodyi)
			mailto += '?body=' + URLdecode(bodyi);
		window.location=mailto; 
	}
	return false;
}


function URLdecode(str) {
	
	try{
    var ret = "";
    for(var i=0;i<str.length;i++) {
        var chr = str.charAt(i);
        if(chr == "+") {
            ret += " ";
        }else if(chr=="%") {
            var asc = str.substring(i+1,i+3);
            if(parseInt("0x"+asc)>0x7f) {
                ret += decodeURI("%"+ str.substring(i+1,i+9));
                i += 8;
            }else {
                ret += String.fromCharCode(parseInt("0x"+asc));
                i += 2;
            }
        }else {
            ret += chr;
        }
    }
    return ret;
	}catch(err){
	return decodeURIComponent(str);	
		}

}
function moreItemFun(){
	$('#nextItem').show();
	var n=$('#imgListsWarp').find('.imgBox').size();
	
	var pn=6;
	var p=Math.ceil($('#imgListsWarp').find('.imgBox').size()/pn);

	$('#nextItem').click(function(){
		if(moreItemPage<=p){
			//if($.browser.msie && parseInt($.browser.version)<7)
			//	$('#imgListsWarp').find('.imgBox:visible').hide();
		//	else
			$('#imgListsWarp').find('.imgBox:visible').hide(500);
			
			for(var i=0;i<pn;i++){
				
					var qk=i+(moreItemPage*pn);
					
					//if($.browser.msie && parseInt($.browser.version)<7){
						//$('#imgListsWarp').find('.imgBox').eq(qk).show();
					//	$('#imgListsWarp').find('.imgBox').eq(qk).show();
					//	$('#imgListsWarp').find('.imgBox').eq(qk).find('img').attr('src',$('#imgListsWarp').find('.imgBox').eq(qk).find('img').attr('value'));
					//}else{
						$('#imgListsWarp').find('.imgBox').eq(qk).show(500);
						$('#imgListsWarp').find('.imgBox').eq(qk).find('img').attr('src',$('#imgListsWarp').find('.imgBox').eq(qk).find('img').attr('value'));
					//}
					//alert($('#imgListsWarp').find('.imgBox').eq((i+(moreItemPage*pn))).html());

			}
			moreItemPage++;

			$('#preItem').show();
			if(moreItemPage==p)  $('#nextItem').hide();
			
		}
		return false;
	});
	$('#preItem').click(function(){
		if(moreItemPage>1){
			//if($.browser.msie && parseInt($.browser.version)<7)
			//	$('#imgListsWarp').find('.imgBox:visible').hide();
			//else
			//	$('#imgListsWarp').find('.imgBox:visible').hide(500);
			for(var i=0;i<pn;i++){
				//if($.browser.msie && parseInt($.browser.version)<7)
				//	$('#imgListsWarp').find('.imgBox').eq((i+(moreItemPage*pn-12))).show();
				//else
					$('#imgListsWarp').find('.imgBox').eq((i+(moreItemPage*pn-12))).show(500);
				$('#imgListsWarp').find('.imgBox').eq((i+(moreItemPage*pn-12))).find('img').attr('src',$('#imgListsWarp').find('.imgBox').eq((i+(moreItemPage*pn-12))).attr('value'));
			}
			moreItemPage--;
			$('#nextItem').show();
			if(moreItemPage==1)  $('#preItem').hide();
		}
		return false;		
	});
	
}

function joinBtn(){
	$('#joinBtn').click(function(){
		$('#nameError').text('');
		$('#mailError').text('');
			$.ajax({
				type: "POST",
				url: "/ajax/joinProxy.php",
				data:{ 'name': $('#name').attr('value'), 'mail': $('#mail').attr('value') },
				dataType: "json",
				timeout : 20000,
				success: function(data) { 
					if(data.check==1)  $('#nameError').text(data.error);
					else if(data.check==2)  $('#mailError').text(data.error);
					else if(data.check==3){
						$('#mailError').text(data.error2);
						$('#nameError').text(data.error1);
					}
					else if(data.check==0){
						$('#joinBox').find('.joinInputTitle').remove();
						$('#joinBox').find('.error').remove();
						$('#joinBox').find('input').remove();
						$('#joinBox').find('#joinBtn').remove();
						$('#joinBox').append('<span id="joinBoxSuss">'+data.error+'</span>');
					}
				}
			}); 
		return false;	
	});
}

function btnclose(){
	$('.btnclose').click(function(){
		$(this).parent().hide();
		return false;	
	});
}

function foo_Tearm(){
	$('#foo_Tearm').click(function(){
		window.open ('/terms/use/en/', 'newwindow', 'height=500, width=580, top=100, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=n o, status=no')
		return false;	
	});
}

function aboutLink(){
	$('#aboutLink').click(function(){
		popDivClose();
		$('#aboutLinkMsg').show();
		return false;	
	});
}

function foo_join(){
	$('#foo_join').click(function(){
		popDivClose();
		$('#joinBox').show();
		return false;	
	});
}

function popDoDiv(){
	$('.popDoDiv').click(function(){
		popDivClose();
		$('#'+$(this).attr('href')).show();
		return false;	
	});
}

function artList(){
	$('.artList').find('a').mousemove( function(){ 
		$('#artWrap').append('<div id="artImgBox"><img src="'+$(this).attr('value')+'"/></div>');
	}); 
	$('.artList').find('a').mouseout( function(){ 
		$('#artWrap').find('#artImgBox').remove();
	}); 
}

function popDoDivFull(){
	$('.popDoDivFull').click(function(){
		try{
			popDivClose();
			$.openDOMWindow({
				overlayColor:'#fff',
				overlayOpacity:'95',
				windowBGColor:'none',
				windowSourceID:'#'+$(this).attr('href')
			});
			if($(this).parent().is('.imgBox')){
				winFullId=$('.imgBox > .popDoDivFull').index($(this));
				winFullIdNum=$('.imgBox > .popDoDivFull').size()-1;
			}else{
				winFullId=$('.imgBox  >  table > tbody > tr > td  > .popDoDivFull').index($(this))>-1?$('.imgBox  >  table > tbody > tr > td  > .popDoDivFull').index($(this)):$('.imgBox  >  table  > tr > td  > .popDoDivFull').index($(this));
				winFullIdNum=($('.imgBox  >  table > tbody > tr > td  > .popDoDivFull').size()?$('.imgBox  >  table > tbody > tr > td  > .popDoDivFull').size():$('.imgBox  >  table > > tr > td  > .popDoDivFull').size())-1;
			}
			if(winFullId < winFullIdNum) $('.nextItemDom').show();
			if(winFullId > 0) $('.preItemDom').show();
			popDoDivFullClick();
			popDoDivFullImg();
			return false;
		}catch(err){}
	});
}

function popDoDivFullClick(){
	$('#DOMWindow').find('.nextItemDom').click(function(){
		if(winFullId<winFullIdNum){
			winFullId++;
			$('#DOMWindow').html($('#'+$('.imgBox > .popDoDivFull').eq(winFullId).attr('href')).html());
			$('.preItemDom').show();
			if(winFullId == winFullIdNum)  $('.nextItemDom').hide();
			popDoDivFullClick();
			popDoDivFullImg();
		}
		return false;
	});
	
	$('#DOMWindow').find('.preItemDom').click(function(){
		if(winFullId>-1){
			winFullId--;
			$('#DOMWindow').html($('#'+$('.imgBox > .popDoDivFull').eq(winFullId).attr('href')).html());
			$('.nextItemDom').show();
			if(winFullId == 0)  $('.preItemDom').hide();
			popDoDivFullClick();
			popDoDivFullImg();
		}
		
		return false;
	});
	
	$('#DOMWindow').find('.closeDOMWindow').click( function(s){$.fn.closeDOMWindow(s);return false;});
}

function popDoDivFullImg(){
	$('#DOMWindow').find('img').attr('src',$('#DOMWindow').find('img').attr('value'));
	$('#DOMWindow').find('img').show();
}

function popDivClose(){
	$('.popwin').hide();
	$('#joinBox').hide();
	$('#aboutLinkMsg').hide();
}


