var $=jQuery;
function dialog(str,cn){
	cn=cn?' '+cn:'';
	if($('#the-frame').length>0){
		$('#the-frame').each(function(){
			if(cn)$(this).addClass(cn);
			else $(this).removeClass().addClass('the-frame');
		}).find('.frame-content').html(str);
	}else{
		$('body',document).append('<div id="the-frame" class="the-frame'+cn+'"style="opacity:0.9;filter:alpha(opacity=90);">'+
			'<table><tbody>'+
				'<tr><td class="t_topleft"></td><td class="t_topborder"></td><td class="t_topright"></td></tr>'+
				'<tr><td class="t_leftborder"></td><td class="t_content"><div class="frame-content">'+str+'</div></td><td class="t_rightborder"></td></tr>'+
				'<tr><td class="t_bottomleft"></td><td class="t_bottomborder"></td><td class="t_bottomright"></td></tr>'+
			'</tbody></table><div class="arrow"></div></div>');
	}
}

$(document).ready(function(){
	/* articles*/
	$('.article-list li,.products-box li,#services-content .little-thumb .sg').hover(
		function(){
			var str='<h3>'+$(this).attr('title')+'</h3>'+
					'<div class="f_c">'+$(this).attr('excerpt')+'</div>',
				ps=$(this).offset();
			dialog(str);
			var h=$('#the-frame').height();
			$('#the-frame').css({
				left:ps.left-30+'px',
				top:ps.top-h+10+'px'
			}).fadeIn(400);
		},
		function(){
			$('#the-frame').css('display','none');
		}
	).find('img').removeAttr('title');
	/* clients */
	var nowpage=1,
		loading=false;
	$('#client-left,#client-right').click(function(){
		if(!loading){
			if(nowpage==1&&this.id=='client-left'){
				alert('This is the first page!');
				return false;
			}
			$.ajax({
				url:'?action=clients&nowpage='+nowpage+'&doaction='+this.id,
				beforeSend:function(){
					document.body.style.cursor='wait';
					loading=true;
					if($('#load-cover').length<1){
						$('#banner').append('<div style="opacity:0.5;filter:alpha(opacity=50);" id="load-cover"></div>')
					}else{
						$('#load-cover').css('display','block');
					}
				},
				type:'GET',
				dataType:'json',
				success:function(json){
					var data='';
					for(var i=0,j=json.length;i<j;i++){
						data+='<div class="client-entry" alt="'+ json[i].posturl +'">'+
							'<div class="client-thumb">'+json[i].thumb+'</div>'+
							'<div class="client-excerpt"><h3>'+json[i].title+'</h3>'+json[i].excerpt+
							'<div class="readmore"><a href="'+json[i].posturl+'">read more</a></div></div></div>';
					}
					$('#clients-content').fadeOut(200,function(){
						$(this).html(data).fadeIn(400);
						$(this).click(function (event) {
							//Stop the default A href event
							event.preventDefault();
							
							//Set Cursor to show busy state
							$("body").css("cursor", "progress");
							
							//Load Ajax content to container
					        $("#container").load($(this).attr("alt") + " #container", function() {
					  			//Scroll down to new content
					  			$.scrollTo("#container", {duration:500});
					  			//Reset Cursor
					  			$("body").css("cursor", "auto");
							});
					 
					    });
					});
					nowpage=parseInt(json[0].nowpage);
				},
				error:function(xhr,msg){
					alert(xhr.responseText)
				},
				complete:function(){
					document.body.style.cursor='auto';
					loading=false;$('#load-cover').css('display','none');
					
				}
			});
		}
	});
	/* downloads */
	var nowpage=1,
		loading=false;
	$('#download-categories-left,#download-categories-right').click(function(){
		if(!loading){
			if(nowpage==1&&this.id=='download-categories-left'){
				alert('This is the first page!');
				return false;
			}
			$.ajax({
				url:'?action=downloads&nowpage='+nowpage+'&doaction='+this.id+'&category='+$('#downloads-content').attr('thecate'),
				beforeSend:function(){
					document.body.style.cursor='wait';
					loading=true;
					if($('#load-cover').length<1){
						$('#banner').append('<div style="opacity:0.5;filter:alpha(opacity=50);" id="load-cover"></div>')
					}else{
						$('#load-cover').css('display','block');
					}
				},
				type:'GET',
				dataType:'json',
				success:function(json){
					var data='';
					for(var i=0,j=json.length;i<j;i++){
						data+='<div class="download-categories-entry'+(i%2?'':' odd')+'" alt="'+ json[i].posturl +'">'+
							'<div class="download-categories-thumb">'+json[i].thumb+'</div>'+
							'<div class="download-categories-excerpt"><h3>'+json[i].title+'</h3>'+json[i].excerpt+
							'<div class="readmore"><a href="'+json[i].posturl+'">more info</a></div></div></div>';
					}
					$('#downloads-content').fadeOut(200,function(){
						$(this).html(data).fadeIn(400);
						$(this).click(function (event) {
							//Stop the default A href event
							event.preventDefault();
							
							//Set Cursor to show busy state
							$("body").css("cursor", "progress");
							
							//Load Ajax content to container
					        $("#container").load($(this).attr("alt") + " #container", function() {
					  			//Scroll down to new content
					  			$.scrollTo("#container", {duration:500});
					  			//Reset Cursor
					  			$("body").css("cursor", "auto");
							});
					 
					    });
					});
					nowpage=parseInt(json[0].nowpage);
				},
				error:function(xhr,msg){
					alert(xhr.responseText)
				},
				complete:function(){
					document.body.style.cursor='auto';
					loading=false;$('#load-cover').css('display','none');
				}
			});
		}
	});
	/* product Category */
	var nowpage=1,
		loading=false;
	$('#product-categories-left,#product-categories-right').click(function(){
		if(!loading){
			if(nowpage==1&&this.id=='product-categories-left'){
				alert('This is the first page!');
				return false;
			}
			$.ajax({
				url:'?action=products&nowpage='+nowpage+'&doaction='+this.id+'&category='+$('#products-content').attr('thecate'),
				beforeSend:function(){
					document.body.style.cursor='wait';
					loading=true;
					if($('#load-cover').length<1){
						$('#banner').append('<div style="opacity:0.5;filter:alpha(opacity=50);" id="load-cover"></div>')
					}else{
						$('#load-cover').css('display','block');
					}
				},
				type:'GET',
				dataType:'json',
				success:function(json){
					var data='';
					for(var i=0,j=json.length;i<j;i++){
						data+='<div class="download-categories-entry'+(i%2?'':' odd')+'" alt="'+ json[i].posturl +'">'+
							'<div class="download-categories-thumb">'+json[i].thumb+'</div>'+
							'<div class="download-categories-excerpt"><h3>'+json[i].title+'</h3>'+json[i].excerpt+
							'<div class="readmore"><a href="'+json[i].posturl+'">more info</a></div></div></div>';
					}
					$('#products-content').fadeOut(200,function(){
						$(this).html(data).fadeIn(400);
						$(this).click(function (event) {
							//Stop the default A href event
							event.preventDefault();
							
							//Set Cursor to show busy state
							$("body").css("cursor", "progress");
							
							//Load Ajax content to container
					        $("#container").load($(this).attr("alt") + " #container", function() {
					  			//Scroll down to new content
					  			$.scrollTo("#container", {duration:500});
					  			//Reset Cursor
					  			$("body").css("cursor", "auto");
							});
					 
					    });
					});
					nowpage=parseInt(json[0].nowpage);
				},
				error:function(xhr,msg){
					alert(xhr.responseText)
				},
				complete:function(){
					document.body.style.cursor='auto';
					loading=false;$('#load-cover').css('display','none');
				}
			});
		}
	});
	
	/* Products Swap */
	$(".productCell").click(function (event) {
		//Stop the default A href event
		event.preventDefault();
		
		//Set Cursor to show busy state
		$("body").css("cursor", "progress");
		
		//Load Ajax content to container
        $("#container").load($(this).attr("alt") + " #container", function() {
  			//Scroll down to new content
  			$.scrollTo("#container", {duration:500});
  			//Reset Cursor
  			$("body").css("cursor", "auto");
		});
 
    });
    
    /* Download Swap */
	$(".downloadCell").click(function (event) {
		//Stop the default A href event
		event.preventDefault();
		
		//Set Cursor to show busy state
		$("body").css("cursor", "progress");
		
		//Load Ajax content to container
        $("#container").load($(this).attr("alt") + " #container", function() {
  			//Scroll down to new content
  			$.scrollTo("#container", {duration:500});
  			//Reset Cursor
  			$("body").css("cursor", "auto");
		});
 
    });
    
    /* Download Swap */
	$(".client-entry").click(function (event) {
		//Stop the default A href event
		event.preventDefault();
		
		//Set Cursor to show busy state
		$("body").css("cursor", "progress");
		
		//Load Ajax content to container
        $("#container").load($(this).attr("alt") + " #container", function() {
  			//Scroll down to new content
  			$.scrollTo("#container", {duration:500});
  			//Reset Cursor
  			$("body").css("cursor", "auto");
		});
 
    });
    
	/* drag */
	$('.products-box li img').mousedown(function(event){
		event=$.event.fix(event);
		var ofs=$(this).offset(),
			id='temp_'+parseInt(Math.random()*99999999),
			startX=event.pageX,
			startY=event.pageY,
			title=$(this).parent().attr('title');
		$(this).clone().appendTo($('body',document)).css({
			position:'absolute',
			top:ofs.top+'px',
			left:ofs.left+'px',
			width:'130px',
			height:'110px'
		}).attr('id',id);
		$(document).mousemove(function(ev){
			var top=parseInt($('#'+id).css('top')),
				left=parseInt($('#'+id).css('left'));
			try{
			event=$.event.fix(ev);
			}catch(e){alert(e)}
			$('#'+id).css({
				left:left+event.pageX-startX+'px',
				top:top+event.pageY-startY+'px'
			});
			startX=event.pageX;
			startY=event.pageY;
			return false;
		});
		$(document).mouseup(function(){
			$(document).mousemove(null);
			if($('#'+id).length<1)return;
			var top=parseInt($('#'+id).css('top')),
				left=parseInt($('#'+id).css('left')),
				ofs=$('#drag-here').offset();
			if(top>ofs.top&&left>ofs.left&&top+110<ofs.top+360&&left+130<ofs.left+420){
				var val=$('#products-list').val();
				if(val.indexOf(title)>-1){
					alert('You have add this product!');
				}
				else{
					$('#drag-here').append('<div class="p_list">'+title+'<div class="close">X</div></div>');
					$('#products-list').val(val+','+title);
				}
			}else{
				$('#'+id).remove();
			}
			$('#'+id).remove();
			return false;
		});
		
		return false;
	});
	$('#drag-here .close').live('click',function(){
		var val=$('#products-list').val(),
			title=$(this).empty().parent().text();
		val=val.replace(','+title,'');
		$(this).parent().remove();
		$('#products-list').val(val);
	});
	$('.buildroom-other .sg,.buildroom-other .fg').click(function(){
		var title=$(this).text(),
			val=$('#categories-list').val();
		if($(this).attr('hasadd')=='1'){
			val=val.replace(','+title,'');
			$('#categories-list').val(val);
			$(this).css('opacity','1');
			$(this).attr('hasadd','0');
		}else{
			$('#categories-list').val(val+','+title);
			$(this).css('opacity','0.5');
			$(this).attr('hasadd','1');
		}
		return false;
	});
	$('#get-quote').click(function(){
		$('#build-form').toggle();
	});
	$('#wp-calendar a').click(function(){
		var str='<h3>'+$(this).attr('title')+'</h3>'+
				'<div class="f_c">'+$(this).attr('excerpt')+'</div>',
			ps=$(this).offset();
		dialog(str,'clendar');
		var h=$('#the-frame').height();
		$('#the-frame .arrow').css({
			top:h/2-15+'px',
			left:'auto',
			right:'-14px',
			bottom:'auto'
		});
		$('#the-frame').css({
			left:ps.left-330+'px',
			top:ps.top-h/2+20+'px'
		}).fadeIn(400);
		return false;
	});
	$('#events-content .details').click(function(){
		var str='<h3>'+$(this).attr('title')+'</h3>'+
				'<div class="f_c">'+$(this).attr('excerpt')+'</div>',
			ps=$(this).offset();
		dialog(str);
		var h=$('#the-frame').height();
		$('#the-frame .arrow').css({
			top:'auto',
			left:'60px',
			right:'auto',
			bottom:'-21px'
		});
		$('#the-frame').css({
			left:ps.left-50+'px',
			top:ps.top-h+'px'
		}).fadeIn(400);
		return false;
	});
	$('#banner_wrap,#container_wrap').click(function(){
		$('#the-frame').fadeOut();
	})
	
	/* home slider */
	if($('#home-slider').length>0){
		var index=1,
			items=$('#home-slider li'),
			btns=$('#pagination li'),
			s=10000,timer,
			loading=false,
			func=function(){
				loading=true;
				items.hide().eq(index).fadeIn(1200,function(){
						index++;
						if(index>=items.length){
							index=0;
						}
						timer=setTimeout(func,s);
						loading=false;
					});
				btns.removeClass('select').eq(index).addClass('select');
			}
		timer=setTimeout(func,s);
		btns.click(function(){
			if(loading)return;
			clearTimeout(timer);
			index=$(this).index();
			func();
		});
		$('#pagination .prev').click(function(){
			if(loading)return;
			clearTimeout(timer);
			index=index-2;
			if(index<0){
				index=index+items.length;
			}
			func();
		});
		$('#pagination .next').click(function(){
			if(loading)return;
			clearTimeout(timer);
			func();
		});
	}
	
	/* footer */
	if($('#f_name').val()==''){
		$('#f_name').val('Please Enter your Name');
	}
	if($('#f_email').val()==''){
		$('#f_email').val('Please Enter your Email');
	}
	$('#f_name').focus(function(){
		if($('#f_name').val()=='Please Enter your Name'){
			$('#f_name').val('');
		}
	});
	$('#f_name').blur(function(){
		if($('#f_name').val()==''){
			$('#f_name').val('Please Enter your Name');
		}
	});
	$('#f_email').focus(function(){
		if($('#f_email').val()=='Please Enter your Email'){
			$('#f_email').val('');
		}
	});
	$('#f_email').blur(function(){
		if($('#f_email').val()==''){
			$('#f_email').val('Please Enter your Email');
		}
	});
	
	/* event share */
	function getParamsOfShareWindow(width, height) {
		return ['toolbar=0,status=0,resizable=1,width=' + width + ',height=' + height + ',left=',(screen.width-width)/2,',top=',(screen.height-height)/2].join('');
	}
	$('#events-content .event-item .share a').click(function(){
		var title=encodeURIComponent($(this).parent().attr('title').substring(0,76)),
			link = encodeURIComponent($(this).parent().attr('e_link')),
			windowName='share',
			cn=$(this).attr('class');
		if(cn=='twitter'){
			var url='http://facebook.com/share.php?u=' + link + '&t=' + title;
			var params = getParamsOfShareWindow(626, 436);
			window.open(url, windowName, params);
		}else if(cn=='facebook'){
			var url = 'http://twitter.com/share?url=' + link + '&text=' + title;
			var params = getParamsOfShareWindow(500, 375);
			window.open(url, windowName, params);
		}
	});
});
document.write('<style type="text/css">#mid-menu li a,#menu li a{text-shadow: 0 1px white;}</style>')
