﻿var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

var baseurl = "http://www.fashionguide.se/gazett/";

var products
var fashionguideDisplayedProducts = new Array(3);

function getFashionguideContent() {
	
	if (window.ActiveXObject) {
			//for IE
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(fashionguideXml);
		} else if (document.implementation && document.implementation.createDocument) {
			//for Mozila
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(fashionguideXml, "text/xml");
		}

	products = xmlDoc.getElementsByTagName("product");
		
	displayRandomProduct(document.getElementById("product-left"));
	displayRandomProduct(document.getElementById("product-right"));
}

function displayRandomProduct(div) {

	if( ! Array.indexOf ) {
	    Array.prototype.indexOf = function( obj ) {
	        for( var i = 0; i < this.length; i++ ) {
	            if( this[i] == obj ) {
	                return i;
	            }
	        }
	        return -1;
	    }
	}

	if( products.length > fashionguideDisplayedProducts.length ) {
		do {
			x = Math.floor( Math.random( ) * products.length );
		} while ( fashionguideDisplayedProducts.indexOf( x ) != -1 )
		
		for( i = fashionguideDisplayedProducts.length -2; i >= 0 ; i-- ) {
			fashionguideDisplayedProducts[i + 1] = fashionguideDisplayedProducts[i];
		}
		fashionguideDisplayedProducts[0] = x;
	}
	else {
		x = Math.floor( Math.random( ) * products.length );
	}
	product = products[x];

	linkurl = baseurl + "goto.asp?id=" + product.getElementsByTagName("id")[0].childNodes[0].nodeValue;
	imagesrc = baseurl + "img/" + product.getElementsByTagName("id")[0].childNodes[0].nodeValue + ".png";
	company = product.getElementsByTagName("company")[0].childNodes[0].nodeValue;
	price = product.getElementsByTagName("price")[0].childNodes[0].nodeValue;
	descriptionText = product.getElementsByTagName("description")[0].childNodes[0].nodeValue;
	titleText = product.getElementsByTagName("title")[0].childNodes[0].nodeValue;

	links = div.getElementsByTagName('a');
	for ( i = 0; i < links.length; i++ ) {
	  links[i].href = linkurl;
	}

	imgs = div.getElementsByTagName('img');
	for ( i = 0; i < imgs.length; i++ ) {
    imgs[i].src = imagesrc;
    imgs[i].alt = titleText;
	};

	ps = div.getElementsByTagName('p');
	for ( i = 0; i < ps.length; i++ ) {
    switch(ps[i].className) {
    	case 'title':
    		ps[i].innerHTML = titleText;
    		break;
    	case 'description':
    		ps[i].innerHTML = descriptionText;
    		break;	
    	case 'company':
    		ps[i].innerHTML = "Finns hos " + company;
    		break;
    	case 'price':
    		ps[i].innerHTML = "Pris: " + price + " kr";
    		break;
    }
	};
}

function moreShopping() {
	displayRandomProduct(document.getElementById('product-left'));
	displayRandomProduct(document.getElementById('product-right'));
}