var loader = $('<div id="loading"><p>Loading... Please wait!</p></div>');
var imgWidth;
var imgHeight;
var intCurrentSlide =0;

$(document).ready(function() 
{
	$(document).pngFix();
	loader.prependTo('body');
	
	$("#headerbk,#footerbk").css({opacity: 0.5});
	
	if($('#pagebkinimg img').length) { $('#pagebkinimg').css({'height': $('#page').height() + 'px'}); $('#pagebkinimg img').css({'width': $(window).width() + 'px'}); $('#pagebkinimg img').show(); }
	if($('.dval').length) { $('.dval').each(function() { $(this).focus(function(){if(this.value==this.defaultValue)this.value='';}); $(this).blur(function(){if(this.value==''){this.value=this.defaultValue;}}); }); }
	
	if($('#c3-1').length) {
		$('#content #c3-2 .vlne').css({'height':'500px'});
	}

	if($('#suimgs a').length) { 
		$("#suimgs a").click(function(){ $.fancybox({'href': $(this).attr('href'),'title':$(this).attr('title'),'transitionIn':'none', 'transitionOut':'none'}); return false; });
	}	
	
	if($('#vimg').length) { 
		$('#vimg').cycle({fx:'scrollHorz', prev:'#prev1', next:'#next1',timeout: 0, startingSlide:1});
	}
	
	if($('#cform').length)
	{
		$('#cfformbtn').click(function() { 
			if($('#name').val() == '' || $('#email').val() == '' || $('#subject').val() == '' || $('#message').val() == '')
				$('#cffrmframe').html('<p class="msg err">Please check the fields</p>');
			else
				$('#cffrm').submit();
			return false; 
		});
		$("#cffrm").submit
		(
			function()
			{
				$('#cffrmframe').html('<p class="msg">Please wait... Sending Mail! </p>');
				strURL = $("#surl").val() + "/send.php";
				var str = $(this).serialize();
				$.ajax(
				{
					type: "POST",
					url: strURL,
					data: str,
					success: 
					function(msgStatus)
					{
						$("#cffrmframe").ajaxComplete(function(event, request, settings)
						{
							$('#cffrmframe').html(msgStatus);
						});
					}
				});
				return false;
			}
		);
	}
});

$(window).load(function() 
{
	loader.hide();
	resetSize();
	$('#page').fadeIn(1);
	$('#headerbk, #footerbk').css({'left': $('#header').offset().left + 'px'});
	sliding();
	$("body").css({"overflow":"auto"});
	if($('#scroller').length) { $('#scroller').jScrollPane({showArrows: true}); }
});

$(window).bind('resize', function () { resetSize(); });

function resetSize()
{
	$('#headerbk, #footerbk').css({'left': $('#header').offset().left + 'px'});
	
	if($('#pagebkinimg img').length)
	{
		$("#pagebkinimg").css("position","fixed");
		$('#pagebkinimg').css({'height': $(window).height() + 'px'});
		$('#pagebkinimg img').css({'width': $(window).width() + 'px'});
	}
	
	if($('#pagebkimg').length)
	{
		$("#pagebkimg").css("position","fixed");
		$("#pagebkimg").css("width",$(window).width() + "px");
		$("#pagebkimg").css("height",$(window).height() + "px");
		
		var iw = 850;
		var ih = 566;
		
		var iww;
		var ihh;
		
		if ($(window).width() > $(window).height()) {
			if (iw > ih) {
				var fRatio = iw/ih;
				iww = $(window).width();
				ihh = Math.round($(window).width() * (1/fRatio));
				var newIh = Math.round($(window).width() * (1/fRatio));
				if(newIh < $(window).height()) {
					var fRatio = ih/iw;
					ihh = $(window).height();
					iww = Math.round($(window).height() * (1/fRatio));
				}
			} else {
				var fRatio = ih/iw;
				ihh = $(window).height();
				iww = Math.round($(window).height() * (1/fRatio));
				}
			} else {
				var fRatio = ih/iw;
				ihh = $(window).height();
				iww = Math.round($(window).height() * (1/fRatio));
			}		
			if (iww > $(window).width()) {
				var this_left = (iww - $(window).width()) / 2;
				$("#pagebkimg").children('img').css({ "top"  : 0, "left" : -this_left });
				if (ihh > $(window).height()) {
					var this_height = (ihh - $(window).height()) / 2;
					$("#pagebkimg").children('img').css({"left" : 0,"top" : -this_height});
			}
		}
	}
	
	$("#pagebkimg").children('img').css("width", iww + "px");
	$("#pagebkimg").children('img').css("height", ihh + 'px');
	$("#pagebkimg").css({"visibility" : "visible"});
	
	imgWidth = iww;
	imgHeight = ihh;
	$('#pagebkimg').cycle({width: iww,height: ihh,startingSlide:intCurrentSlide, after: onAfter});
}


function sliding()
{
	$('#pagebkimg').cycle({fit:true,height: imgHeight,width:imgWidth, timeout: 6500, fx: "fade", speed: 700, delay: 0, sort: 1, sync: 1, random: 0, pause: 0, nowrap: 0, cleartype: true, cleartypenobg: false, after: onAfter, startingSlide:0 });
	$('.serlinkleft').click(function() { intID = parseInt($(this).attr('title')); slidingshow(intID);$('#pagebkimg').cycle(intID); return false; });
	$('.serlinkright').click(function() { intID = parseInt($(this).attr('title')); slidingshow(intID);$('#pagebkimg').cycle(intID); return false; });
}

function onAfter(curr,next,opts) { intCurrentSlide = opts.currSlide; slidingshow(opts.currSlide); }

function slidingshow(intID)
{
	strLinkID = '#serlink #sl' + intID;
	strCntID = '#sercnt #sc' + intID;
	$('#serlink li').removeClass('show');
	$('#sercnt li').removeClass('show');
	$(strCntID).addClass('show');
	$(strLinkID).addClass('show');
}
