/*
 * netStarterFramework - interactive "Feature Items" widget base on http://jqueryglobe.com/article/feature_list/
 * Version: 1.2.2 (04/12/2011)
 * Copyright (c) 2010 netStarter
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.4+
 * By: Shabith Ishan 
 *
 * v 1.2.2
 * minor bug fixed.
*/

jQuery.loadXML = function(siteURL, callback, pageId, childId)
{
		
	$.fn.loadMainXML = function()
	{
		//category XML load
		$.ajax({
			
			dataType: "xml",
			cache: true,
			success: function(data, textStatus, XMLHttpRequest)
					{
						
						childrenID = $(data).find('TemporaryTable:eq('+cId+')').find('ChildId').text();
						
						$.fn.loadMediaXML();
					},
			error: function(XMLHttpRequest, textStatus, errorThrown)
					{
						throw new Error('XML loading Error - '+'status : '+textStatus+'\n error : '+errorThrown);
					},
					url: site+categoryURL+pid
					//url: "test.xml"
					
					
			
			});
	};

	$.fn.loadMediaXML = function()
	{

		$.ajax({
			dataType: "xml",
			cache:true,
			success: function(data, textStatus, XMLHttpRequest)
				{
					// If the user passed a callback, and it
					// is a function, call it, and send through the data var.
					if ( typeof callback === 'function') {
					
						callback(data);
					 }// Else, Maybe we requested a site that doesn't exist, and nothing returned.
					else throw new Error('No callback function specified or invaid function');
				},
			error: function(XMLHttpRequest, textStatus, errorThrown)
					{
						throw new Error('XML loading Error - '+'status : '+textStatus+'\n error : '+errorThrown);
					},
		
				
				url: site+mediaURL+childrenID
			
			});
	};
	
	
	$.fn.crossDomainXML = function(cId)
	{
		//alert('cId - '+childrenID);
		var clientMedia = new XMLProxy();
			// url of rss feed
		//	var url = 'http://rss.news.yahoo.com/rss/topstories';
			var url = site+mediaURL+cId;
			// retrieve RSS Feed

		  clientMedia.getXML(url, function(str){
			  	var $xml = $.fn.xmlDOM(str.replace('?',''));
				//$('#alert').text(site+mediaURL+cId);
				
				callback($xml);
			  }); 

	};
	
	
	$.fn.loadDirectXML = function(siteURL, callback)
	{
		
		$.ajax({
			dataType: "xml",
			cache:true,
			success: function(data, textStatus, XMLHttpRequest)
				{
					// If the user passed a callback, and it
					// is a function, call it, and send through the data var.
					if ( typeof callback === 'function') {
						
						callback(data);
					 }// Else, Maybe we requested a site that doesn't exist, and nothing returned.
					else throw new Error('No callback function specified or invaid function');
				},
			error: function(XMLHttpRequest, textStatus, errorThrown)
					{
						throw new Error('XML loading Error - '+'status : '+textStatus+'\n error : '+errorThrown);
					},
		
				
				url: siteURL
			
			});
	};
	
	
	/*!
	 * jQuery xmlDOM Plugin v1.0
	 * http://outwestmedia.com/jquery-plugins/xmldom/
	 *
	 * Released: 2009-04-06
	 * Version: 1.0
	 *
	 */
	
	$.fn.xmlDOM = function(xml, onErrorFn) {
		try {
			var xmlDoc 	= ( new DOMParser() ).parseFromString( xml, 'text/xml' );
			if ( $.isXMLDoc( xmlDoc ) ) {
				var err = $('parsererror', xmlDoc);
				if ( err.length == 1 ) {
					throw('Error: ' + $(xmlDoc).text() );
				}
			} else {
				throw('Unable to parse XML');
			}
		} catch( e ) {
			var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
			if ( $.isFunction( onErrorFn ) ) {
				onErrorFn( msg );
			} else {
				$(document).trigger('xmlParseError', [ msg ]);
			}
			return $([]);
		}
		return $( xmlDoc );
	};

	
	//variables
	var categoryURL = "/includes_templates/processmanagement/extract/media/photogallery/category.aspx?SiteItemId=";
	var mediaURL = "/includes_templates/processmanagement/extract/media/photogallery/images.aspx?SitemediacategoryId=";
	
	var proxyPath = "js/";
	
	var pid = pageId;
	var site = siteURL;
	
	var childrenID;
	
	//set default values
    childId = typeof(childId) != "undefined" ? childId : 0;
	
	var cId = childId;
	
	if(site!=="")
	{
		if(site.match(/http:\/\//gi) && typeof(pid)=="undefined")
		{
			
			$.getScript(proxyPath+"XMLProxy.js", function(){
				// create an instance of RSSClient
				var client2 = new XMLProxy();
			
				// url of rss feed
				//	var url = 'http://rss.news.yahoo.com/rss/topstories';
				var url = site;
				// retrieve RSS Feed
					 
					  client2.getXML(url, function(str){
						  
						  	// IE bug fix
						  	var $xml = $.fn.xmlDOM(str.replace('?',''));
							//alert('xml - '+$(xml).find('TemporaryTable:eq('+cId+')').length);
							//console.log($xml);
							callback($xml);
						  }); 

			
			
			});
		}
		else if(typeof(pid) == "undefined" || pid =="")
		{
			
			
			$.fn.loadDirectXML(site, callback);
			
		}
		else
		{
			$.getScript(proxyPath+"XMLProxy.js", function(){
				// create an instance of RSSClient
				var client = new XMLProxy();
			
				// url of rss feed
				//	var url = 'http://rss.news.yahoo.com/rss/topstories';
				var url = site+categoryURL+pid;
				// retrieve RSS Feed
					 
					  client.getXML(url, function(str){
						  
						  	// IE bug fix
						  	var $xml = $.fn.xmlDOM(str.replace('?',''));
							//alert('xml - '+$(xml).find('TemporaryTable:eq('+cId+')').length);
							//console.log($xml);
							childrenID = $xml.find('TemporaryTable:eq('+cId+')').find('ChildId').text();
							
							$.fn.crossDomainXML(childrenID);
						  }); 

			
			
			});
				
		}
		
		
	}else
	{
		$.fn.loadMainXML();
	}

}

$(function(){
	// IE DOMParser wrapper
	if ( window['DOMParser'] == undefined && window.ActiveXObject ) {
		DOMParser = function() { };
		DOMParser.prototype.parseFromString = function( xmlString ) {
			var doc = new ActiveXObject('Microsoft.XMLDOM');
	        doc.async = 'false';
	        doc.loadXML( xmlString );
			return doc;
		};
	}

})


