(function( $ ){
    $.fn.Center = function() {
	var popupWidth = this.width();
	this.css({    
	    "left": windowWidth/2-popupWidth/2  
	});  
    };    
    $.fn.Submenu = function() {
	$menu=this.find(".j-menu");
	$menu.find("a").each(function(index){
	    $(this).click(function(){
		if($(this).is(".j-clickable")) {
		    $menu.find("li").removeClass("j-active");
		    $menu.find("li.active").addClass("j-not-active");
		    $(".j-submenu").hide();
		    if(!$(this).parent().hasClass("j-active")) {
			
			$(this).parent().addClass("j-active");			
		    }
		    if(index>2) i=1; else i=2; 
		    $(".b-menus .j-submenu:nth-child("+i+")").show();
		    $(".l-overlay").show();
		}
		else {
		    $(".j-submenu").hide();
		    $(".l-overlay").hide();
		    $(this).parent().removeClass("j-active");
		}
	    });
	    
	});
	$(".l-overlay").click(function(){
	    var t = $(event.target);
	    if(		
		!t.is($(".j-submenu"))  &&
		!t.parents().is($(".j-submenu")) 
            )
                {
		$(".j-submenu").hide();
		$menu.find("li.j-not-active").removeClass("j-not-active");
		$("a.j-clickable").parent().removeClass("j-active");
		$(".l-overlay").hide();
		}
	});
    };

    $.fn.print = function(){
	// NOTE: We are trimming the jQuery collection down to the
	// first element in the collection.
	if (this.size() > 1){
		this.eq( 0 ).print();
		return;
	} else if (!this.size()){
		return;
	}
 
	// ASSERT: At this point, we know that the current jQuery
	// collection (as defined by THIS), contains only one
	// printable element.
 
	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());
 
	// Create an iFrame with the new name.
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
 
	// Hide the frame (sort of) and attach to the body.
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( $( "body:first" ) )
	;
 
	// Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];
 
	// Get a reference to the DOM in the new frame.
	var objDoc = objFrame.document;
 
	// Grab all the style tags and copy to the new
	// document so that we capture look and feel of
	// the current document.
 
	// Create a temp document DIV to hold the style tags.
	// This is the only way I could find to get the style
	// tags into IE.
	var jStyleDiv = $( "<div>" ).append(
		$( "style" ).clone()
		);
	var styleSheet='h2{clear: left;} .l-monitor-inline, br {display: none;} .b-shedule-col {overflow: hidden; margin-bottom: 30px} a {text-decoration: none; color: #000} body{font-family: Arial, sans-serif} p {margin-bottom: 0} .b-shedule-col-time {float: left; clear: left; font-weight: bold} .b-shedule-col-event{padding-left: 20px; float: left; width: 500px}';
	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<body>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
	//objDoc.write( jStyleDiv.html() );
	objDoc.write( "<style>" );
	objDoc.write( styleSheet );
	objDoc.write( "</style>" );
	objDoc.write( "</head>" );
	objDoc.write( this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();
 
	// Print the document.
	objFrame.focus();
	objFrame.print();
 
	// Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
	}
    
})( jQuery );

$(document).ready(function(){
    $('.j-load-link').click(function(){
	href=$(this).attr('href');
	$("#map").load(href+' #content');
	scroll=$("#map").offset().top-156;
	$('html, body').animate({scrollTop: scroll+'px'}, 400, function(){
});
	return false;
    });
    if($('.slideShow').length>0) {$('.slideShow').slideShow({
				interval: 0,
				speed: 1000
			    });}
    /*if($('.partnersShow').length>0) {$('.partnersShow').slideShow({
	interval: 0,
	speed: 1000
    });}*/
    
   
    $('#coda-slider-1').codaSlider();
    $('a.j-popup-link').colorbox({rel:'gal', opacity: 0.75, current: "", close: "", next: "", previous: ""});
    $('a.j-popup-link-collect').colorbox({rel:'gal-collect', opacity: 0.75, current: "", close: "", next: "", previous: ""});
    $('a.j-popup-link-collection').colorbox({rel:'gal-collection', opacity: 0.75, current: "", close: "", next: "", previous: ""});
    $('a.j-popup-link-accredit').colorbox({inline:true, opacity: 0.75, current: "", close: "", next: "", previous: ""});
    $(".j-more").click(function(){
	$(this).parent().children(".g-hidden").slideDown();
	$(this).hide();
	return false;
    });
    $(".l-top").Submenu();
    $(".j-next-step").click(function(){
	if(CheckAccredit()) {
	    $(".j-step-second").show();
	    $(this).parent().hide();
	}
	return false;
    });
    $("#my_contest_my_form .form-submit").click(function(){
	if(CheckContest()) {
	    return true;
	}
	return false;
    });
    $(".error").live('focusin', function(){
	$(".error").removeClass("error");
    });
    
    $(function(){
	$( "a.b-print" ).attr( "href", "javascript:void(0)" ).click(
	    function(){
		$( ".printable" ).print();
		return( false );
	    }
	);}
    )
    
});
function CheckContest() {
    $flag=true;
    if(!$("input[name='name']").val()) {
	$("input[name='name']").addClass("error");
	$flag=false;
    }
    if(!$("textarea[name='answer']").val()) {
	$("textarea[name='answer']").addClass("error");
	$flag=false;
    }
    if(!(/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test($("input[name='email']").val())){
	$("input[name='email']").addClass("error");
	$flag=false;
    };
    return $flag;
}
function CheckAccredit() {
    $flag=true;
    if(!$("input[name='name']").val()) {
	$("input[name='name']").addClass("error");
	$flag=false;
    }
    if(!$("input[name='title']").val()) {
	$("input[name='title']").addClass("error");
	$flag=false;
    }
    if(!$("input[name='city']").val()) {
	$("input[name='city']").addClass("error");
	$flag=false;
    }
    if(!$("input[name='company']").val()) {
	$("input[name='company']").addClass("error");
	$flag=false;
    }
     if(!$("input[name='country']").val()) {
	$("input[name='country']").addClass("error");
	$flag=false;
    }
    if(!$("input[name='website']").val()) {
	$("input[name='website']").addClass("error");
	$flag=false;
    }
    if(!$("input[name='mobile']").val()) {
	$("input[name='mobile']").addClass("error");
	$flag=false;
    }
    if(!$("input[name='phone']").val()) {
	$("input[name='phone']").addClass("error");
	$flag=false;
    }
    if(!(/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test($("input[name='email']").val())){
	$("input[name='email']").addClass("error");
	$flag=false;
    };
    return $flag;
}
var metas = document.getElementsByTagName('meta');
  var i;
  if (navigator.userAgent.match(/iPhone/i)) {
    for (i=0; i<metas.length; i++) {
      if (metas[i].name == "viewport") {
        metas[i].content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
      }
    }
    document.addEventListener("gesturestart", gestureStart, false);
  }
  function gestureStart() {
    for (i=0; i<metas.length; i++) {
      if (metas[i].name == "viewport") {
        metas[i].content = "width=device-width, minimum-scale=0.25, maximum-scale=1.6";
      }
    }
  }
  $(function(){
    $('#slides').slides({
	effect: 'fade',
	play: 5000,
	paginationClass: 'navigation',
	generatePagination: true,
	fadeSpeed: 1200,
	crossfade: true,
	randomize: true
    });
});
