jQuery.noConflict();


/**
   * PK Init Vars
*/

	var pk_stage_width = 0;
	var pk_stage_height = 0;
	var pk_news_list_box_height = 80;
	var pk_news_list_box_visible = 4;
	var pk_deep_link_change_news = false;
	var pk_back_click = false;

	var pk_news_max_height = pk_news_box_max_height;
	var pk_news_label = pk_news_title_label;
	var pk_button_back_label = pk_news_button_back_label;

	var pk_news = [];


/*
  * Document / Window Events
*/

	jQuery(document).ready(function() {

		jQuery("body").removeClass("pk_standard_pages");
		jQuery("#wrapper").removeClass("pk_center_box").show();

		pk_stage_width = jQuery(window).width();
		pk_stage_height = jQuery(window).height();

		pkInitDeepLink();

		pkCreateMainMenu();
		pkCreatePagination();
		pkCreateTitles();
		pkCreateNews();

	});

	jQuery(window).resize(function() {

		pk_stage_width = jQuery(window).width();
		pk_stage_height = jQuery(window).height();

		pkResizeMainMenu();
		pkResizePagination();
		pkResizeNews();

	});


/**
   * PK Init Deep Link
*/

	function pkInitDeepLink() {

		jQuery.address.change(function(event) {

			if(event.value == "" || event.value == "/") {

				pkManageNewsDetail("close");

			}

 		}).externalChange(function(event) {

			if(event.pathNames.length > 0) {

				pk_deep_link_change_news = true;

				jQuery(".pk_caption a").each(function() {

					if(jQuery(this).attr("data-deepLinking") == event.pathNames[0]) {

						jQuery(this).trigger("click", [true]);

					}
				});

			}

 		});

	}


/**
   * PK Create Main Menu
*/

	function pkCreateMainMenu() {

		var menu = jQuery("nav");
		var logo = jQuery("header h1");

		menu.hide();
		logo.hide();

		menu.css({ "position" : "absolute", "left" : Math.round((pk_stage_width / 2) - (menu.outerWidth() / 2)) + "px", "top" : 0 + "px"}).show();
		logo.css({ "position" : "absolute", "left" : "0px", "top" : Math.round(pk_stage_height / 8) + "px" }).show();

	}


/**
   * PK Resize Main Menu
*/

	function pkResizeMainMenu() {

		var menu = jQuery("nav");
		var logo = jQuery("header h1");

		menu.css({ "left" : Math.round((pk_stage_width / 2) - (menu.outerWidth() / 2)) + "px", "top" : 0 + "px" });
		logo.css({ "position" : "absolute", "left" : "0px", "top" : Math.round(pk_stage_height / 8) + "px" }).show();

	}


/**
   * PK Create Pagination
*/

	function pkCreatePagination() {

		var footer = jQuery("footer");

		if(jQuery(".pk_footer_info", footer).length > 0) {

			var controls = jQuery("#pk_controls");
			var controls_width = controls.width();

			controls.css({ "width" : "auto", "visibility" : "hidden" });
			jQuery(".pk_footer_info", controls).css({ "display" : "inline" });
			footer.css({ "position" : "absolute", "top" : Math.round(pk_stage_height - 30) + "px" });

			setTimeout(function() {
				var controls_width = jQuery(".pk_footer_info", controls).width() + jQuery(".pk_footer_sn", controls).outerWidth() + 50;
				controls.css({ "position" : "absolute", "left" : Math.round((pk_stage_width / 2) - (controls_width / 2)) + "px", "width" : controls_width + "px", "visibility" : "visible" });
			}, 400);

		} else {

			var pagination = jQuery("#pk_controls");
			var pagination_width = pagination.outerWidth();

			pagination.css({ "position" : "absolute", "left" : Math.round((pk_stage_width / 2) - (pagination_width / 2)) + "px", "top" : 0 + "px" });
			footer.css({ "position" : "absolute", "top" : Math.round(pk_stage_height - 30) + "px" });

		}

	}


/**
   * PK Resize Pagination
*/

	function pkResizePagination() {

		var footer = jQuery("footer");

		if(jQuery(".pk_footer_info", footer).length > 0) {

			var controls = jQuery("#pk_controls");
			var controls_width = controls.width();

			controls.css({ "position" : "absolute", "left" : Math.round((pk_stage_width / 2) - (controls_width / 2)) + "px", "top" : 0 + "px" });
			footer.css({ "position" : "absolute", "top" : Math.round(pk_stage_height - 30) + "px" });

		} else {

			var pagination = jQuery("#pk_controls");
			var pagination_width = pagination.outerWidth();

			pagination.css({ "position" : "absolute", "left" : Math.round((pk_stage_width / 2) - (pagination_width / 2)) + "px", "top" : 0 + "px" });
			footer.css({ "position" : "absolute", "top" : Math.round(pk_stage_height - 30) + "px" });

		}

	}


/**
   * PK Create Titles
*/

	function pkCreateTitles() {

		jQuery("#pk_news_header").show().append('<div class="pk_title"><p>' + pk_news_label + '</p></div><div class="pk_title"><a href="#" id="backbtn" title="">' + pk_button_back_label + '</a></div>');

		var label_1 = jQuery("#pk_news_header").find("div:first");
		var label_2 = jQuery("#pk_news_header").find("div:last");

		label_1.css({ "margin-left" : "68px" });
		label_2.css({ "margin-left" : "60px", "cursor" : "pointer" }).hide();

		label_2.find("a").removeAttr("href");
		label_2.find("a").click(function() {

			pk_back_click = true;

			jQuery.address.value("/");

		});

	}


/**
   * PK Change Title
*/

	function pkChangeTitle(action) {

		var label_1 = jQuery("#pk_news_header").find("div:first");
		var label_2 = jQuery("#pk_news_header").find("div:last");

		if(action == "open") {

			if(pk_deep_link_change_news == false) {

				label_1.fadeOut(400);
				label_2.delay(100).fadeIn(800);

			} else {

				label_1.hide();
				label_2.show();

			}

		} else {

			if(pk_back_click == true) {

				label_2.fadeOut(400);
				label_1.delay(400).fadeIn(800);

			} else {

				label_2.hide();
				label_1.show();

			}

		}

	}


/**
   * PK Create News
*/

	function pkCreateNews() {

		pkResizeObject("#pk_news");
		pkPositionObject("#pk_news");

		jQuery("#pk_news_list h3").hide();
		jQuery("#pk_news").addClass("pk_shadow");
		jQuery("#pk_news_list").css({ "position" : "absolute" });
		jQuery("#pk_news_wrapper").css({ "margin-top" : 40 + "px" , "height" : (jQuery("#pk_news").height() - 80) + "px" });

		jQuery("#pk_news_list li").each(function(e) {

			pk_news[e] = jQuery(".pk_entry", this).html();

			jQuery(".pk_entry", this).hide();
			jQuery(".pk_caption", this).css({ "display" : "block", "height" : pk_news_list_box_height + "px" });

			jQuery(".pk_caption a", this).css({ "display" : "block" });
			jQuery(".pk_caption a", this).click(function() {

				pkAttachNewsDetail(e);
				pkManageNewsDetail("open");

			});

			jQuery(".pk_caption a", this).address(function() {

				return jQuery(this).attr('data-deepLinking');

			});

		});

		var tot_news = jQuery("#pk_news_list li").length;
		var header_height = jQuery("#pk_news_header").height();
		var list_height = (jQuery("#pk_news_list li .pk_caption").outerHeight() * pk_news_list_box_visible) + (1 * pk_news_list_box_visible);
		var margin_top = Math.round((jQuery("#pk_news_wrapper").height() / 2) - ((list_height + header_height) / 2));

		jQuery('#pk_news_list').css({ "height" : list_height + "px" });
		jQuery("#pk_news_content").css({ "margin-top" : margin_top + "px", "height" : (list_height + header_height) + "px" });

		if(jQuery("#pk_news_wrapper").height() <= jQuery('#pk_news_content').height()) {

			jQuery("#pk_news_content").css({ "margin-top" : 0 + "px" });
			jQuery('#pk_news_list').css({ "height" : (jQuery("#pk_news_wrapper").height() - header_height) + "px" });

		} else {

			(margin_top <= 0) ? jQuery("#pk_news_content").css({ "margin-top" : 0 + "px" }) : jQuery("#pk_news_content").css({ "margin-top" : margin_top + "px" });
			jQuery('#pk_news_list').css({ "height" : list_height + "px" });

		}

		(jQuery("#pk_news_list ul").height() > jQuery('#pk_news_list').height()) ? jQuery("#pk_news_list ul").css({ "width" : 580 + "px" }) : jQuery("#pk_news_list ul").css({ "width" : 600 + "px" });

		jQuery('#pk_news_list').jScrollPane();

	}


/**
   * PK Resize News
*/

	function pkResizeNews() {

		pkResizeObject("#pk_news");
		pkPositionObject("#pk_news");

		jQuery("#pk_news_wrapper").css({ "height" : (jQuery("#pk_news").height() - 80) + "px" });

		var tot_news = jQuery("#pk_news_list li").length;
		var header_height = jQuery("#pk_news_header").height();
		var list_height = (jQuery("#pk_news_list li .pk_caption").outerHeight() * pk_news_list_box_visible) + (1 * pk_news_list_box_visible);
		var margin_top = Math.round((jQuery("#pk_news_wrapper").height() / 2) - ((list_height + header_height) / 2));

		if(jQuery("#pk_news_wrapper").height() <= jQuery('#pk_news_content').height()) {

			jQuery("#pk_news_content").css({ "margin-top" : 0 + "px" });
			jQuery('#pk_news_list, #pk_news_detail').css({ "height" : (jQuery("#pk_news_wrapper").height() - header_height) + "px" });

		} else {

			(margin_top <= 0) ? jQuery("#pk_news_content").css({ "margin-top" : 0 + "px" }) : jQuery("#pk_news_content").css({ "margin-top" : margin_top + "px" });
			jQuery('#pk_news_list, #pk_news_detail').css({ "height" : list_height + "px" });

		}

		(jQuery("#pk_news_list ul").height() > jQuery('#pk_news_list').height()) ? jQuery("#pk_news_list ul").css({ "width" : 580 + "px" }) : jQuery("#pk_news_list ul").css({ "width" : 600 + "px" });
		(jQuery("#pk_news_text").height() > jQuery('#pk_news_list').height()) ? jQuery("#pk_news_text").css({ "width" : 580 + "px" }) : jQuery("#pk_news_text").css({ "width" : 600 + "px" });

		jQuery('#pk_news_list').jScrollPane();
		jQuery('#pk_news_detail').jScrollPane();

	}


/**
   * PK Attach News Detail
*/

	function pkAttachNewsDetail(id) {

		jQuery("#pk_news_content").append('<div id="pk_news_detail"><div id="pk_news_text" style="width:600px; overflow:hidden;"></div></div>');

		if(jQuery("#pk_news_wrapper").height() <= jQuery('#pk_news_content').height()) {

			jQuery('#pk_news_detail').css({ "oveflow" : "hidden", "width" : 600 + "px", "height" : (jQuery("#pk_news_wrapper").height() - jQuery("#pk_news_header").height()) + "px" });

		} else {

			jQuery("#pk_news_detail").css({ "oveflow" : "hidden", "width" : 600 + "px", "height" : (jQuery("#pk_news_list li .pk_caption").outerHeight() * pk_news_list_box_visible) + (1 * pk_news_list_box_visible) + "px" });

		}

		jQuery("#pk_news_text").html(pk_news[id]);
		(jQuery("#pk_news_text").height() > jQuery('#pk_news_list').height()) ? jQuery("#pk_news_text").css({ "width" : 580 + "px" }) : jQuery("#pk_news_text").css({ "width" : 600 + "px" });
		jQuery(".pk_zoom_icon, .pk_play_icon, .pk_page_icon, .pk_link_icon").pk_image_rollover();
		jQuery('#pk_news_detail').jScrollPane();
        if(typeof checkpw != "undefined") $('#pk_news_detail').admin();

	}


/**
   * PK Manage News Detail
*/

	function pkManageNewsDetail(action, id) {

		pkChangeTitle(action);

		if(action == "open") {

			if(pk_deep_link_change_news == false) {

				jQuery("#pk_news_detail").stop().css({ "margin-left" : 0 + "px" , "width" :  0 + "px" });
				jQuery("#pk_news_detail").stop().animate({ "margin-left" : 60 + "px" , "width" : 600 + "px" }, 800, "easeInOutExpo");
				jQuery("#pk_news_list").stop().animate({ "margin-left" : 720 + "px" , "width" : 0 + "px" }, 800, "easeInOutExpo");

			} else {

				jQuery("#pk_news_detail").stop().css({ "margin-left" : 60 + "px" , "width" : 600 + "px" });
				jQuery("#pk_news_list").stop().css({ "margin-left" : 720 + "px" , "width" : 0 + "px" });

			}
			
			$('.categs').hide(100);
			
		} else {

			if(pk_back_click == true) {

				jQuery("#pk_news_list").stop().animate({ "margin-left" : 60 + "px" , "width" : 600 + "px" }, 800, "easeInOutExpo");
				jQuery("#pk_news_detail").stop().animate({ "oveflow" : "hidden", "margin-left" : 0 + "px" , "width" : 0 + "px" }, 800, "easeInOutExpo", function() {

					jQuery("#pk_news_detail").remove();
					pk_back_click = false;

				});

			} else {

				jQuery("#pk_news_list").stop().css({ "margin-left" : 60 + "px" , "width" : 600 + "px" });
				jQuery("#pk_news_detail").stop().remove();

			}
			
			$('.categs').show(100);

		}

		pk_deep_link_change_news = false;

	}

	jQuery("#pk_news_detail").live('mouseenter',function(){
		if(jQuery.address.value() == "/" || jQuery.address.value() == "")
		jQuery(this).remove()
	});
	
/**
   * PK Resize Object
*/

	function pkResizeObject(object) {

		var obj = jQuery(object);

		var max_width = 910;
		var max_height = Math.round(jQuery(window).height() - 120);

		(max_height >= pk_news_max_height) ? obj.css({ "width" : max_width + "px", "height" : pk_news_max_height + "px" }) : obj.css({ "width" : max_width + "px", "height" : max_height + "px" });

	}


/**
   * PK Position Object
*/

	function pkPositionObject(object) {

		var obj = jQuery(object);

		var pk_stage_width = jQuery(window).width();
		var pk_stage_height = jQuery(window).height();

		obj.css({"position" : "absolute", "left" : (pk_stage_width / 2) - (obj.width() / 2), "top" : (pk_stage_height / 2) - (obj.height() / 2) });

	}
